in reply to Union of arrays

You already have correct answers. I like to do it like this:

my (%hsh, @union_arr); @hsh{@arr_in_1, @arr_in_2} = (); @union_arr = keys %hsh;

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Union of arrays
by thpfft (Chaplain) on Oct 26, 2002 at 12:06 UTC

    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