in reply to Comparing Two Arrays

Here's a solution from the Perl Cookbook which gets the union, intersection, and symmetric difference.
my @a = (2,4,6,8,10); my @b = (1,2,3,4,5); my @isect = my @diff = my @union = (); my %count; $count{$_}++ for (@a, @b); @union = keys %count; for (keys %count) { push @{$count{$_} == 2 ? \@isect : \@diff}, $_; }

Replies are listed 'Best First'.
Re^2: Comparing Two Arrays
by exussum0 (Vicar) on Feb 24, 2005 at 03:45 UTC
    Just a tip, you may wish to invert your ?: clause if you ever wish to handle n cases.

    push @{$count{$_} != 1 ? \@diff : \@isec}, $_;

    ----
    Give me strength for today.. I will not talk it away..
    Just for a moment.. It will burn through the clouds.. and shine down on me.