Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > difference in arrays/hashes

Reply
Thread Tools

difference in arrays/hashes

 
 
Me
Guest
Posts: n/a
 
      08-12-2005
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.
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      08-12-2005
Me wrote:
> 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

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-12-2005
Me wrote:
> 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;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
James E Keenan
Guest
Posts: n/a
 
      08-12-2005
Me wrote:
> 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

 
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
difference between Wifi repeater and range extender? AdminKen Wireless Networking 4 02-11-2011 04:05 AM
wireless speed difference =?Utf-8?B?dG9t?= Wireless Networking 6 06-02-2005 02:16 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
difference between 802.3b and 802.11b Tony Warburton Wireless Networking 2 11-25-2004 10:27 PM
Difference : WEP open / shared ? Michael van den Berg Wireless Networking 1 08-18-2004 07:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57