This seems to work...
use strict;
# two lists for the comparison
my @orders = (1,5,7,9,3,1,55,23);
my @stash = (4,12,8,0,2,7,9,3,4,13);
# hash of unique numbers
my %join;
# add all of the numbers from both lists to %join.
# turn on the 1 "bit" for @orders, and "2" for @stash.
# this will give numbers that only appear in @orders
# a value of 1, if only in @stash a value of 2, and
# if it appear in both a value of 3.
$join{$_} = 1 for (@orders);
$join{$_} |= 2 for (@stash);
# grab the unique numbers from %join
# and sort them numerically
my @unique_keys = sort {$a<=>$b} keys %join;
# grep out matches (a value of 3) and non-matches.
my @matches = grep {$join{$_} == 3} @unique_keys;
my @non_matches = grep {$join{$_} != 3} @unique_keys;
print "MATCHES: @matches\n";
print "NON MATCH: @non_matches\n";
Rob
-----Original Message-----
From: Trevor Morrison [mailto:[EMAIL PROTECTED]
Sent: Friday, August 08, 2003 9:09 AM
To: [EMAIL PROTECTED]
Subject: Simple question
Hi,
I am trying to compare two arrays to find common numbers in both. For the
numbers that are not common to both, I want to write them to a file for my
review. I have included the code below, but it is not working as intended.
I know that this is a real basic question, but I have been staring at this
for hours and am not seeing it. I am assuming that there must me a module
that will do this, but I have decided to reinvent the wheel!
TIA
Trevor
#!/usr/local/bin/perl
#============
# Main script
#------------
use strict;
#use warnings;
#Use Perl's Database Interface (DBI) with the NySQL module to connect the
Maverick database
use DBI;
my %attr = (PrintError => 1, RaiseError => 1);
my $dbname='maverick';
my $dbuser='root';
my $dbpass='';
my
$dbh=DBI->connect('DBI:mysql:database=maverick;host=localhost;port=3306',"$d
buser","$dbpass", \%attr) || die "Unable to connect to database
maverick on
localhost: $DBI::errstr\n";
my $email = qw(c:\\maverick\\trevor_trial2.txt);
our @orders;
our @stash;
our $order;
our $stash;
our $carryover;
our $found = "true";
open(ORDER,$email) or die "Error opening \"$email\": $!\n";
my $n = 0;
my $sth =$dbh->prepare("SELECT order_number FROM miva_orders");
$sth->execute();
while (<ORDER>) {
if ($_=~ /Order\s+Number\s+:\s+(.*)$/i ) {
$orders[$n] = $1;
$n += 1;
#print $n;
}
}
foreach my $order(@orders) {
#print "$order\n";
}
while (my $array_ref = $sth->fetchrow_arrayref) {
push @stash, [ @$array_ref ];
}
# Check to see if order numbers are equal and if not print them to a file
for review
open (CHECKER, ">c:\\maverick\\ordercheck.txt") || die $!;
STASH: foreach $order(@orders) {
foreach my $stash
(@stash) {
if
($order == @$stash)
$found = "true";
#next STASH;
}
}
if
($found eq "false") {
print $order;
print CHECKER $order . "\n";
#$found == 0;
}
}
# Disconnnect from the database
$sth->finish;
$dbh->disconnect;
close(CHECKER);
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]