Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: What does !$saw{$_}++ means

by Anonymous Monk
on Jan 26, 2005 at 09:56 UTC ( [id://425157]=note: print w/replies, xml ) Need Help??


in reply to Re: What does !$saw{$_}++ means
in thread What does !$saw{$_}++ means

With no change to the idiom, I'd write that as,
my %saw; my @out = grep { ! $saw{$_}++ } @in;
But that extends the lifetime of %saw. If later in the same (or an inner) block, you need to use the same construct, you have to use a different name for the hash, or do a %saw = () - which will then cause errors if you remove the first construct (until you my the newer construct).

I like to write it as:

my @out = do {my %saw; grep !$saw{$_}++, @in};
which doesn't leak the name of the temporary array.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-16 18:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found