http://www.perlmonks.org?node_id=919451


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