Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by Jasper (Chaplain)
on Aug 04, 2003 at 22:21 UTC ( #280823=note: print w/replies, xml ) Need Help??


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

You can also grep lists for certain 'numbers' of entries (so if you wanted only the items that were in a list twice)
@doubles = grep ++$seen{$_} == 2, @list;
Jasper

Replies are listed 'Best First'.
Re: Re: Re: Perl Idioms Explained - keys %{{map{$_=>1}@list}}
by cees (Curate) on Aug 05, 2003 at 03:32 UTC

    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

Re^3: Perl Idioms Explained - keys %{{map{$_=>1}@list}}
by Aristotle (Chancellor) on Aug 05, 2003 at 17:51 UTC
    No, that won't fly. You need
    $seen{$_}++ for @list; my @doubles = grep $seen{$_} == 2, @list;

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://280823]
help
Chatterbox?
[LanX]: ehm ... fetchall_hashref
[Jenda]: my int @a = ^5_000_000 Coerces the argument to Numeric, and generates a range from 0 up to (but excluding) the argument.
[Jenda]: The apparent design principle of Perl6 operators was "Let's confuse 'em dudes." Whose braindead idea was this particular operator and the three hundred around it?

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2018-07-16 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (344 votes). Check out past polls.

    Notices?