Comparing Arays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Taxi Driver

    Comparing Arays

    Hi Everyone -
    Can I get your help with this, it is driving me crazy.
    I have 2 arrays listed below:
    @req[0][0]=75
    @req[1][0]=76
    @req[2][0]=77
    @req[2][0]=78
    ---
    @bid[0][0]=75
    @bid[1][0]=76
    @bid[2][0]=80

    I need to find all cases where a number in @req is NOT in @bid. In
    this example 77 and 78.

    Thank you
  • Matt Garrish

    #2
    Re: Comparing Arays


    "Taxi Driver" <7sumari@mail.g oo.ne.jp> wrote in message
    news:nd7ri1lv7k 4fkasccp44gr6mv tm86gpjp0@4ax.c om...[color=blue]
    > Hi Everyone -
    > Can I get your help with this, it is driving me crazy.
    > I have 2 arrays listed below:
    > @req[0][0]=75[/color]

    What are these things? Did you mean to write $req[0][0]? If so, please bear
    in mind that you should always post real code.
    [color=blue]
    > @req[1][0]=76
    > @req[2][0]=77
    > @req[2][0]=78[/color]

    Why are you using multi-dimensional arrays when there is only one entry for
    each? Or are there more entries? It's hard to give you useful help if you
    don't present the problem clearly (like why you repeated @req[2][0], for
    example).
    [color=blue]
    > ---
    > @bid[0][0]=75
    > @bid[1][0]=76
    > @bid[2][0]=80
    >
    > I need to find all cases where a number in @req is NOT in @bid. In
    > this example 77 and 78.
    >[/color]

    Hashes are your friend in situations like this. Assuming you have a real
    mutlidimensiona l array (and all the entries in @bid and @req contain
    references to arrays):

    my %chk;

    for my $x (0..$#bid) {
    for my $y (0..$#{$bid[$x]}) {
    $chk{$bid[$x][$y]} = 1;
    }
    }

    for my $i (0..$#req) {
    for my $j (0..$#{$req[$i]}) {
    print "Not found: $req[$i][$j]\n" unless $chk{$req[$i][$j]};
    }
    }


    Matt


    Comment

    • John Bokma

      #3
      Re: Comparing Arays

      Taxi Driver <7sumari@mail.g oo.ne.jp> wrote:
      [color=blue]
      > Hi Everyone -
      > Can I get your help with this, it is driving me crazy.
      > I have 2 arrays listed below:
      > @req[0][0]=75
      > @req[1][0]=76
      > @req[2][0]=77
      > @req[2][0]=78
      > ---
      > @bid[0][0]=75
      > @bid[1][0]=76
      > @bid[2][0]=80[/color]

      Uhm...

      You mean:

      use strict;
      use warnings;

      my @req = ( 75 .. 78 );
      my @bid = ( 75, 76, 80 );
      [color=blue]
      > I need to find all cases where a number in @req is NOT in @bid. In
      > this example 77 and 78.[/color]

      my %test;
      @test{ @bid } = ();

      my @not_in;
      exists $test{ $_ } or push @not_in, $_ for @req;

      print join( ', ', @not_in ), "\n";

      --
      John Small Perl scripts: http://johnbokma.com/perl/
      Perl programmer available: http://castleamber.com/
      Happy Customers: http://castleamber.com/testimonials.html

      Comment

      • Jim Gibson

        #4
        Re: Comparing Arays

        In article <nd7ri1lv7k4fka sccp44gr6mvtm86 gpjp0@4ax.com>, Taxi Driver
        <7sumari@mail.g oo.ne.jp> wrote:
        [color=blue]
        > Hi Everyone -
        > Can I get your help with this, it is driving me crazy.
        > I have 2 arrays listed below:
        > @req[0][0]=75
        > @req[1][0]=76
        > @req[2][0]=77
        > @req[2][0]=78
        > ---
        > @bid[0][0]=75
        > @bid[1][0]=76
        > @bid[2][0]=80
        >
        > I need to find all cases where a number in @req is NOT in @bid. In
        > this example 77 and 78.[/color]

        perldoc -q "How do I compute the difference of two arrays"

        Posted Via Usenet.com Premium Usenet Newsgroup Services
        ----------------------------------------------------------
        ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
        ----------------------------------------------------------
        Best Usenet Service Providers 2025 ranked by Newsgroup Access Newsservers, Usenet Search, Features & Free Trial. Add VPN for privacy.

        Comment

        • Taxi Driver

          #5
          Re: Comparing Arays

          Thank you. That is exactly what I was looking for.
          I apologize for not posting code.

          TD

          Matt Garrish:
          "Matt Garrish" <matthew.garris h@sympatico.ca> a écrit dans le
          message :[color=blue]
          >
          >"Taxi Driver" <7sumari@mail.g oo.ne.jp> wrote in message
          >news:nd7ri1lv7 k4fkasccp44gr6m vtm86gpjp0@4ax. com...[color=green]
          >> Hi Everyone -
          >> Can I get your help with this, it is driving me crazy.
          >> I have 2 arrays listed below:
          >> @req[0][0]=75[/color]
          >
          >What are these things? Did you mean to write $req[0][0]? If so, please bear
          >in mind that you should always post real code.
          >[color=green]
          >> @req[1][0]=76
          >> @req[2][0]=77
          >> @req[2][0]=78[/color]
          >
          >Why are you using multi-dimensional arrays when there is only one entry for
          >each? Or are there more entries? It's hard to give you useful help if you
          >don't present the problem clearly (like why you repeated @req[2][0], for
          >example).
          >[color=green]
          >> ---
          >> @bid[0][0]=75
          >> @bid[1][0]=76
          >> @bid[2][0]=80
          >>
          >> I need to find all cases where a number in @req is NOT in @bid. In
          >> this example 77 and 78.
          >>[/color]
          >
          >Hashes are your friend in situations like this. Assuming you have a real
          >mutlidimension al array (and all the entries in @bid and @req contain
          >references to arrays):
          >
          >my %chk;
          >
          >for my $x (0..$#bid) {
          > for my $y (0..$#{$bid[$x]}) {
          > $chk{$bid[$x][$y]} = 1;
          > }
          >}
          >
          >for my $i (0..$#req) {
          > for my $j (0..$#{$req[$i]}) {
          > print "Not found: $req[$i][$j]\n" unless $chk{$req[$i][$j]};
          > }
          >}
          >
          >
          >Matt
          >[/color]

          Comment

          • Taxi Driver

            #6
            Re: Comparing Arays

            It all works great until I added a second variable (ts) to the first
            MYSQL Select statement. Then I get multiple valuse for ts instead of
            one.
            ---------------
            #!/usr/bin/perl
            use DBI;
            #Declare arrays to hold results
            my @data;
            my @data2;
            my (@requests) = ();
            my (@bids) = ();

            # Database information
            $db="X";
            $host="X";
            $port="X";
            $userid="X";
            $passwd="X";
            $connectionInfo ="DBI:mysql:dat abase=$db;$host :$port";

            # Make connection to database
            $dbh = DBI->connect($conne ctionInfo,$user id,$passwd) or die "Couldn't
            connect to db. " . $sth->errstr;

            # Prepare and execute query
            $query = "SELECT id, ts FROM insrequest WHERE status=0 ORDER BY id";
            $sth = $dbh->prepare($query );
            $sth->execute()or die "Couldn't execute statement. " . $sth->errstr;

            # Read the matching records and print them out.
            while (@data = $sth->fetchrow_array ()) {
            my $id = $data[0];
            push(@requests, [@data]);
            # print "Row: @data\n";
            }

            if ($sth->rows == 0) {
            print "No jobs found.\n\n";
            }

            # Second query
            $query2 = "SELECT DISTINCT jobnum FROM insbid1 ORDER BY jobnum";
            $sth = $dbh->prepare($query 2);
            $sth->execute()or die "Couldn't execute statement. " . $sth->errstr;

            # Read the matching records and print them out.
            while (@data2 = $sth->fetchrow_array ()) {
            my $jobnum = $data2[0];
            push(@bids, [@data2]);
            }

            if ($sth->rows == 0) {
            print "No bids found insbid1.\n\n";
            }

            $sth->finish();

            # Disconnect from database
            $dbh->disconnect;

            # Comparison
            print "Not found:\n";
            my %chk;

            for my $x (0..$#bids) {
            for my $y (0..$#{$bids[$x]}) {
            $chk{$bids[$x][$y]} = 1;
            }
            }

            for my $i (0..$#requests) {
            for my $j (0..$#{$request s[$i]}) {
            print "\t$request s[$i][$j]\n" unless $chk{$requests[$i][$j]};
            }
            }
            ----------
            Matt Garrish:
            "Matt Garrish" <matthew.garris h@sympatico.ca> a écrit dans le
            message :[color=blue]
            >
            >"Taxi Driver" <7sumari@mail.g oo.ne.jp> wrote in message
            >news:nd7ri1lv7 k4fkasccp44gr6m vtm86gpjp0@4ax. com...[color=green]
            >> Hi Everyone -
            >> Can I get your help with this, it is driving me crazy.
            >> I have 2 arrays listed below:
            >> @req[0][0]=75[/color]
            >
            >What are these things? Did you mean to write $req[0][0]? If so, please bear
            >in mind that you should always post real code.
            >[color=green]
            >> @req[1][0]=76
            >> @req[2][0]=77
            >> @req[2][0]=78[/color]
            >
            >Why are you using multi-dimensional arrays when there is only one entry for
            >each? Or are there more entries? It's hard to give you useful help if you
            >don't present the problem clearly (like why you repeated @req[2][0], for
            >example).
            >[color=green]
            >> ---
            >> @bid[0][0]=75
            >> @bid[1][0]=76
            >> @bid[2][0]=80
            >>
            >> I need to find all cases where a number in @req is NOT in @bid. In
            >> this example 77 and 78.
            >>[/color]
            >
            >Hashes are your friend in situations like this. Assuming you have a real
            >mutlidimension al array (and all the entries in @bid and @req contain
            >references to arrays):
            >
            >my %chk;
            >
            >for my $x (0..$#bid) {
            > for my $y (0..$#{$bid[$x]}) {
            > $chk{$bid[$x][$y]} = 1;
            > }
            >}
            >
            >for my $i (0..$#req) {
            > for my $j (0..$#{$req[$i]}) {
            > print "Not found: $req[$i][$j]\n" unless $chk{$req[$i][$j]};
            > }
            >}
            >
            >
            >Matt
            >[/color]

            Comment

            • John Bokma

              #7
              Re: Comparing Arays

              Taxi Driver <7sumari@mail.g oo.ne.jp> wrote:
              [color=blue]
              > It all works great until I added a second variable (ts) to the first
              > MYSQL Select statement. Then I get multiple valuse for ts instead of
              > one.
              > ---------------
              > #!/usr/bin/perl
              > use DBI;[/color]

              most people stop looking now, because you forgot use strict; and use
              warnings;

              Also, it looks like you are attempting to program a join in Perl, instead
              of using the database to do this. Try to give an exact description of your
              problem based on the database table(s), I am sure it can be done in SQL.

              Finally, *don't* top post.

              --
              John Small Perl scripts: http://johnbokma.com/perl/
              Perl programmer available: http://castleamber.com/
              Happy Customers: http://castleamber.com/testimonials.html

              Comment

              Working...