in reply to Re: Union of arrays
in thread Union of arrays
The hash slice is very neat, but you're going to lose sequence that way, which might matter. How about a nearly-one-liner:
my %seen; my @union = grep { not $seen{$_}++ } (@arr_in_1, @arr_in_2);
which also skips duplicates in the existing arrays, as yours would.
Btw, does the hash slice - if, indeed, that's what it is - cut out this loop altogether, or does it just conceal it? If the former then I imagine this approach is a lot less efficient.
ananda: the perl cookbook deals with this kind of question very well, iirc.
update: code tweak
|
---|