Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Extracting elements in one array but not in another

by emazep (Priest)
on Oct 23, 2004 at 06:50 UTC ( #401778=note: print w/replies, xml ) Need Help??

in reply to Extracting elements in one array but not in another

To find elements that are in one array but not in another, there is a nice loopless solution from the Perl Cookbook which uses the delete function:
my %seen; @seen{ @original_1 } = (); delete @seen{ @original_2 }; my @original_1_only = keys %seen;
Of course there is also a loopy solution, should you prefer it:
my %seen; my @original_1_only; @seen{ @original_2 } = (); foreach (@original_1) { push(@original_1_only, $_) unless exists $seen{$_}; };
Stick with these.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://401778]
[thezip]: My doggies love it when it snows
[perldigious]: As long as you are a good "boy scout" stevieb, more power to you... but I like my Jeep, and don't like the idea of rolling it or crashing it in to anything. :-)
[thezip]: Me, not as much.
[perldigious]: Mine too thezip, especially my German Shepherd. He looks like he is about to die of heat exhaustion all summer, so he goes nuts when winter comes and he can play in the snow.
[stevieb]: my main off-road vehicle nowadays is a strengthened mercedes ML320. I'm very hard on it, but I've never had any real issues at all (I love how small and light it is, and it has a very, very good centre of gravity for how I roll)
[perldigious]: "for how I roll"... ahhh, I see what you did there stevieb.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (13)
As of 2016-12-06 16:54 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (112 votes). Check out past polls.