Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by ZlR (Chaplain)
on Jan 26, 2005 at 08:46 UTC ( [id://425142]=note: print w/replies, xml ) Need Help??


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

Hello,

Here's my interpretation :

%saw is a hash.
$saw{$_} is the value in this hash associated with the key $_
$saw{$_}++ increases this value by 1
!$saw{$_} is a logical which means "there is no value associated with the key $_ in %saw" .

Now, this is used inside a grep applied to @in : this means that $_ will take each of the value in @in . Let's take the first value of @in : obviously it's not yet in %saw so the conditional !$saw{$_} is TRUE (it's a double negation) . Therefore grep validates and this first value goes into @out.

At this time a little magic happens : after evaluation of !$saw{$_} the ++ is applied . I'm only guessing this happens because of some precedence of ! over ++ .

So what if the second element of @in is the same as the first ? Well, since ++ happened , $saw{$_} will have a value of 1 and therefore !$saw{$_} will be FALSE : you will not get this repetition in the final @out .

Hope this helps,
ZlR .

Question : I just checked in the camel book:
the ! opertor has an arity of 1 and is right associative
the ++ operator also has an arity of 1 but is not associative.
I'm not sure then why the !$saw{$_}++ is correctly evaluated since they have the same arity.

Answer by ysth : nothing to do with arity , it's just that ! has higher precedence than ++ .

Replies are listed 'Best First'.
Re^2: What does !$saw{$_}++ means
by tphyahoo (Vicar) on Jan 26, 2005 at 09:05 UTC
    !saw{$_} should have been !$saw{$_}

    (Maybe obvious, but since this is about deobfuscating an idiom, I thought I'd post anyway.)

      Yep ! Corrected :)

Re^2: What does !$saw{$_}++ means
by ysth (Canon) on Jan 26, 2005 at 10:12 UTC
    I only have a Camel II, which doesn't list arity in the operator table, so I'm not sure exactly what you are seeing. However, arity has nothing to do with precedence; !$seen{$_}++ works because ++ has higher precedence than !.
      Yes, i checked again (camel 3) and this time i understood the table corectly : precedence is shown by the way the operators are ordered, while "arity" is the number of argument they can take. thx .

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-23 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found