Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Uniq and sort in perl

by Preceptor (Chaplain)
on Jul 15, 2012 at 11:26 UTC ( #981893=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Uniq and sort in perl
in thread Uniq and sort in perl

What is happening in that one liner:

print "$vm, $filer, \n" if ! $seen{"$vm:$filler"}++

It's quite an elegant piece of code, with a lot going on. So let's break it down:

  • The hash called '%seen' is being checked - to see if it contains a key called "$vm:$filler". The value of this is being tested for being 'false'. (because there's 'if !' which is 'if not') - which is literally 'if not seen'
  • So if $seen{"$vm:filler"} is _not_ 'true' - we haven't seen it, and so we print it.
  • And then that ++ kicks in, to post increment, 'adding one' to $seen{"$vm:$filler"}' - so the first time we try doing that, it's going to be undefined (and thus false) triggering a print. Second time, it'll be 'non zero' and so evaluate as 'true'.
For bonus points, you can probably do:
foreach my $key ( keys %seen ) { print "$key was seen $seen{$key} times\n"; }
Does that make it any clearer?


Comment on Re^3: Uniq and sort in perl
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-08-23 13:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (173 votes), past polls