473,394 Members | 1,290 Online

# 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
Sep 18 '05 #1
6 3430

"Taxi Driver" <7s*****@mail.goo.ne.jp> wrote in message
news:nd********************************@4ax.com...
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]}) {
}
}
Matt
Sep 18 '05 #2
Taxi Driver <7s*****@mail.goo.ne.jp> 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

Sep 18 '05 #3
In article <nd********************************@4ax.com>, Taxi Driver
<7s*****@mail.goo.ne.jp> 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

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

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

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Sep 18 '05 #4
Thank you. That is exactly what I was looking for.
I apologize for not posting code.

TD

Matt Garrish:
"Matt Garrish" <ma*************@sympatico.ca> a écrit dans le
message :

"Taxi Driver" <7s*****@mail.goo.ne.jp> wrote in message
news:nd********************************@4ax.com.. .
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]}) {
}
}
Matt

Sep 18 '05 #5
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
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" <ma*************@sympatico.ca> a écrit dans le
message :

"Taxi Driver" <7s*****@mail.goo.ne.jp> wrote in message
news:nd********************************@4ax.com.. .
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]}) {
}
}
Matt

Sep 18 '05 #6
Taxi Driver <7s*****@mail.goo.ne.jp> 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

Sep 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 5 by: beliavsky | last post by: By mistake I coded something like print ("1" > 1) and got the result "True". Comparing an integer and a string seems meaningless to me, and I would prefer to have an exception thrown. Can... 41 by: Odd-R. | last post by: I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and... 88 by: William Krick | last post by: I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm... 2 by: Manny Chohan | last post by: Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny 3 by: Ricky W. Hunt | last post by: How does VB.NET determine comparing vs. assigning? For instance, if "checkbox1.checked = True" it only checks the value but leaves it as it whereas if you have "checkbox1.checked = True" by... 19 by: Dennis | last post by: I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =... 4 by: Frank | last post by: Hello, Developing an app where the user fills out a sometimes quite lengthy form of chkboxes, txtboxes, radbtns, etc. User responses are saved to a mySql db, which the user can later edit. When... 5 by: ma740988 | last post by: There's a need for me to move around at specified offsets within memory. As as a result - long story short - unsigned char* is the type of choice. At issue: Consider the case ( test code ) where... 20 by: Bill Pursell | last post by: This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is... 2 by: Pugi! | last post by: hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function... 0 by: Charles Arthur | last post by: How do i turn on java script on a villaon, callus and itel keypad mobile phone 0 by: ryjfgjl | last post by: If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming... 0 by: ryjfgjl | last post by: In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files... 1 by: nemocccc | last post by: hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions? 1 by: Sonnysonu | last post by: This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to... 0 by: marktang | last post by: ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,... 0 by: Oralloy | last post by: Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,... 0 by: jinu1996 | last post by: In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven... 0 by: tracyyun | last post by: Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...