Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Difference between two arrays - is there a better way?

by bart (Canon)
on Aug 09, 2011 at 11:11 UTC ( #919451=note: print w/replies, xml ) Need Help??


in reply to Difference between two arrays - is there a better way?

Here is my own "clever way", and you can even use it on more than 2 lists.
my %presence; my $b = 1; foreach my $ary (\@list1, \@list2, \@list3) { foreach(@$ary) { $presence{$_} |= $b; } } continue { $b *= 2; }
Now you can grep in keys %presence. Each value will indicate in what lists the item was found, as a bit mask. Examples:
@list1only = grep { $presence{$_} == 1 } keys %presence; @list2only = grep { $presence{$_} == 2 } keys %presence; @list3only = grep { $presence{$_} == 4 } keys %presence; @lists1and2only = grep { $presence{$_} == 1+2 } keys %presence; @inall = grep { $presence{$_} == 1+2+4 } keys %presence; # etc

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://919451]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2019-10-13 23:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?