Me said:
I have two data sets from two sources, each set of results is stored in
files which are ared into @file1 and @file2.
"man perlfaq4" gives an exampe of an intersection of the two. My
problems is, I need to know which elements of @file1 are not in @file2
and the other way around. How can I keep track of this using the example
from the man page.
Set all the elements from one array as keys to a hash, with an
arbitrary value (say 1). Loop through the second array, keeping track
of which elements do not exists as keys to the hash. These elements
are the difference of the second array "minus" the first. Then reverse
the procedure (after clearing the hash) to find the opposite
difference.
The code given in the perlfaq you found (and *thank you* for searching
the perlfaq before posting!) will not translate immediately to what you
want, as it stores all elements from both arrays into the hash, and
then loops through the elements of the hash, rather than either
individual array.
#!/usr/bin/perl
use strict;
use warnings;
my @a1 = qw/a 1 b 2 c 3/;
my @a2 = qw/3 c 4 d 5 e/;
my %h;
$h{$_} = 1 for @a1;
my @diff = grep {!exists $h{$_} } @a2;
print "Elements in a2 not in a1: @diff\n";
%h = ();
$h{$_} = 1 for @a2;
@diff = grep {!exists $h{$_} } @a1;
print "Elements in a1 not in a2: @diff\n";
__END__
Elements in a2 not in a1: 4 d 5 e
Elements in a1 not in a2: a 1 b 2
Paul Lalli