Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Re: Re: Perl Idioms Explained - keys %{{map{$_=>1}@list}}

by cees (Curate)
on Aug 05, 2003 at 03:32 UTC ( #280874=note: print w/ replies, xml ) Need Help??

in reply to Re: Re: Perl Idioms Explained - keys %{{map{$_=>1}@list}}
in thread Perl Idioms Explained - keys %{{map{$_=>1}@list}}

That would be 2 or more times right? Once ++$seen{$_} == 2 is true, you are immediatelly copying $_ to @doubles. So if it appeared a third time, you couldn't magically remove it again. Your code would be clearer if you used >= to emphasize that.

The following would probably do if you only wanted entries with exactly 2 occurences:

@doubles = grep $seen{$_} == 2, grep !$seen{$_}++, @list;

But it is not pretty, or obvious. The first grep counts all occurences and only passes the first found entry to the next grep which will check to see how many were actually found.

There must be a better way!

- Cees

Comment on Re: Re: Re: Perl Idioms Explained - keys %{{map{$_=>1}@list}}
Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2015-11-28 14:12 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (742 votes), past polls