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

Re^3: Explain the code

by gopalr (Priest)
on Jul 24, 2008 at 13:08 UTC ( #699868=note: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
Re^4: Explain the code
by olus (Curate) on Jul 24, 2008 at 13:12 UTC

    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 };
Re^4: Explain the code
by moritz (Cardinal) on Jul 24, 2008 at 13:09 UTC
    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.
Re^4: Explain the code
by FunkyMonk (Canon) on Jul 24, 2008 at 13:11 UTC
    $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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://699868]
[Your Mother]: Windows doesn't even recognize them as far as that goes.
[Lady_Aleena]: Your Mother, thanks. I had a feeling it would be no.
[Your Mother]: :P
[Lady_Aleena]: Your Mother, it is a good thing I am no on Windows.
[Lady_Aleena]: s/no/not/;
[Lady_Aleena]: My blasted web host will not put perl 5.22 in /usr/bin/perl, it is in /usr/local/cpanel/ 3rdparty/bin/perl
Lady_Aleena has been banging her head against this wall for years now with no success on breaking the wall.

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2017-04-28 06:31 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (519 votes). Check out past polls.