Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: What is %_

by Preceptor (Deacon)
on Sep 29, 2015 at 14:45 UTC ( #1143344=note: print w/replies, xml ) Need Help??


in reply to What is %_

At risk of a spot of necromancy - I've just been handed a use of %_:

@list = grep { ! $_{$_} } @list;

It's neat because it's self contained. Although it does seem to leave some namespace pollution as a result

Found via: http://stackoverflow.com/questions/32846792/what-is-in-perl

Replies are listed 'Best First'.
Re^2: What is %_
by LanX (Sage) on Sep 29, 2015 at 20:38 UTC
    > It's neat because it's self contained. Although it does seem to leave some namespace pollution as a result

    If you want to use this "unique" construction multiple times, it'll break if you don't clean up %_ afterwards.

    So its neat for a one liner, in every other situation I'd rather prefer an explicit my %seen or importing uniq from List::MoreUtils .

    update

    Btw your code does nothing, you forgot the ++ part.

    @list = grep { !$_{$_}++ } @list;

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2022-11-30 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?