in reply to How to splice out multiple entries from an array
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)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: How to splice out multiple entries from an array
by demerphq (Chancellor) on Oct 24, 2002 at 13:02 UTC | |
|
Re: Re: How to splice out multiple entries from an array
by demerphq (Chancellor) on Oct 24, 2002 at 08:57 UTC | |
|
Re: Re: How to splice out multiple entries from an array
by Ya (Initiate) on Oct 24, 2002 at 11:31 UTC |