Found in /usr/share/perl/5.6.1/pod/perlfaq4.pod How do I compute the difference of two arrays? How do I compute the intersection of two arrays? Use a hash. Here's code to do both and more. It assumes that each element is unique in a given array: @union = @intersection = @difference = (); %count = (); foreach $element (@array1, @array2) { $count{$eleme +nt}++ } foreach $element (keys %count) { push @union, $element; push @{ $count{$element} > 1 ? \@intersection : + \@difference }, $element; } Note that this is the symmetric difference, that is, all elements in either A or in B but not in both. Think of it as an xor operation.
"large inputs" tend to take a "long time" to process. Take that as a given in life -- something that's implied by the laws of physics
The only real caveat, is that sometimes when dealing with large inputs, the nature of the problem lends itself to a solution in which you can get intermediate results as you go, without having to first see all of the results. (ie: find all lines of input tha match a certain pattern).
Set operations do not fall into that category of problem -- you pretty much have to have both sets, in their entirety, available to you at the same time in order to find the difference (or intersection, or union)
In reply to Re: How to splice out multiple entries from an array
by hossman
in thread How to splice out multiple entries from an array
by Ya
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |