Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Comparing Arays

Reply
Thread Tools

Comparing Arays

 
 
Taxi Driver
Guest
Posts: n/a
 
      09-18-2005
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
 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      09-18-2005

"Taxi Driver" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Everyone -
> Can I get your help with this, it is driving me crazy.
> I have 2 arrays listed below:
> @req[0][0]=75


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.

> @req[1][0]=76
> @req[2][0]=77
> @req[2][0]=78


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).

> ---
> @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.
>


Hashes are your friend in situations like this. Assuming you have a real
mutlidimensional 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


 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      09-18-2005
Taxi Driver <(E-Mail Removed)> wrote:

> 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


Uhm...

You mean:

use strict;
use warnings;

my @req = ( 75 .. 78 );
my @bid = ( 75, 76, 80 );

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


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

 
Reply With Quote
 
Taxi Driver
Guest
Posts: n/a
 
      09-18-2005
Thank you. That is exactly what I was looking for.
I apologize for not posting code.

TD

Matt Garrish:
"Matt Garrish" <(E-Mail Removed)> a écrit dans le
message :
>
>"Taxi Driver" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed).. .
>> Hi Everyone -
>> Can I get your help with this, it is driving me crazy.
>> I have 2 arrays listed below:
>> @req[0][0]=75

>
>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.
>
>> @req[1][0]=76
>> @req[2][0]=77
>> @req[2][0]=78

>
>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).
>
>> ---
>> @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.
>>

>
>Hashes are your friend in situations like this. Assuming you have a real
>mutlidimensional 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
>


 
Reply With Quote
 
Taxi Driver
Guest
Posts: n/a
 
      09-18-2005
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:database=$db;$host:$por t";

# Make connection to database
$dbh = DBI->connect($connectionInfo,$userid,$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($query2);
$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..$#{$requests[$i]}) {
print "\t$requests[$i][$j]\n" unless $chk{$requests[$i][$j]};
}
}
----------
Matt Garrish:
"Matt Garrish" <(E-Mail Removed)> a écrit dans le
message :
>
>"Taxi Driver" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed).. .
>> Hi Everyone -
>> Can I get your help with this, it is driving me crazy.
>> I have 2 arrays listed below:
>> @req[0][0]=75

>
>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.
>
>> @req[1][0]=76
>> @req[2][0]=77
>> @req[2][0]=78

>
>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).
>
>> ---
>> @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.
>>

>
>Hashes are your friend in situations like this. Assuming you have a real
>mutlidimensional 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
>


 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      09-18-2005
Taxi Driver <(E-Mail Removed)> wrote:

> 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;


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

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      09-18-2005
John Bokma wrote:
> Taxi Driver <(E-Mail Removed)> wrote:
>
> > 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

>
> Uhm...
>
> You mean:
>
> use strict;
> use warnings;
>
> my @req = ( 75 .. 78 );
> my @bid = ( 75, 76, 80 );
>
> > I need to find all cases where a number in @req is NOT in @bid. In
> > this example 77 and 78.

>
> my %test;
> @test{ @bid } = ();
>
> my @not_in;
> exists $test{ $_ } or push @not_in, $_ for @req;
>
> print join( ', ', @not_in ), "\n";


In Ruby:

req = ( 75 .. 78 ).to_a
bid = [ 75, 76, 80 ]
p req - bid

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bug in list of matrics represended as arays..... Djuro.Tost@gmail.com C Programming 19 07-04-2008 04:39 PM
comparing the array in parallel srinukasam VHDL 3 07-30-2005 12:27 PM
comparing the array for generic parameters srinukasam VHDL 3 06-30-2005 07:40 PM
comparing the contents of memory srinukasam VHDL 5 06-23-2005 07:49 PM
[VHDL] Comparing entity and component declarations M.D. van de Burgwal VHDL 3 10-07-2004 08:58 AM



Advertisments