difference in arrays/hashes

M

Me

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

Paul Lalli

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
 
G

Gunnar Hjalmarsson

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.

The most important part of the "perldoc -q intersection" answer is the
first sentence: "Use a hash." ;-)

The rest is an example, and you need to adopt it to fit your particular
situation.

my %count;
foreach my $element (@file1, @file2) { $count{$element}++ }
my @unique1 = grep $count{$_} == 1, @file1;
my @unique2 = grep $count{$_} == 1, @file2;
 
J

James E Keenan

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.

My CPAN module List::Compare
(http://search.cpan.org/dist/List-Compare/) provides a number of ways
to do this. Look for the get_unique and get_complement methods and
functions.

Jim Keenan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top