Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Explain the code

by olus (Curate)
on Jul 24, 2008 at 12:58 UTC ( [id://699863]=note: print w/replies, xml ) Need Help??


in reply to Re: Explain the code
in thread Explain the code

%seen will also have the number of occurrences of every element of @a.

Replies are listed 'Best First'.
Re^3: Explain the code
by gopalr (Priest) on Jul 24, 2008 at 13:08 UTC

    How? If the array (@a) has duplicate values, hash will eliminate that.

      Noticed the ++ in !$seen{$_}++?

      perl -MData::Dumper -e '@a=(1, 2, 1, 2, 3); my @result = grep { !$seen +{$_}++ } @a; print Dumper(\%seen);' $VAR1 = { '1' => 2, '3' => 1, '2' => 2 };
      The ++ is evaluated for every item in @a, grep only checks its return value and picks (or not picks) the element based on that value.
      $seen{4} = 3 means that 4 has been seen 3 times.

      Unless I state otherwise, all my code runs with strict and warnings

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-03-28 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found