|P is for Practical|
How do I get an array which is the logical AND of the elements of two other arrays?by ibanix (Hermit)
|on Feb 25, 2006 at 22:21 UTC||Need Help??|
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:
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;