in reply to Re: Difference of array
in thread Difference of array

Any idea why the answer in the FAQs makes such an IMHO needless assumption?

I think your code doesn't compute the difference, and the union also isn't what you'd normally define as union (even if you swap @intersection and @union)...

my @array1 = qw(foo foo bar baz); my @array2 = qw(bar grmpf asdf); my (@intersection, @union); my %count; @count{@array1} = undef; for (@array2) { if (exists $count{$_}) { push @union, $_; } else { push @intersection, $_; } } use Data::Dumper; print Dumper \@intersection, \@union; __END__ $VAR1 = [ 'grmpf', 'asdf' ]; $VAR2 = [ 'bar' ];

Replies are listed 'Best First'.
Re^3: Difference of array
by moritz (Cardinal) on May 15, 2009 at 09:18 UTC
    Yes, you're totally right. If one allows duplicates, the union is just @union = @array1, @array2.