Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Re: Union of arrays

by thpfft (Chaplain)
on Oct 26, 2002 at 12:06 UTC ( [id://208220]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://208220]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found