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

Re: What does 'next if $hash{$elem}++;' mean?

by dragonchild (Archbishop)
on Feb 17, 2006 at 15:32 UTC ( [id://530971]=note: print w/replies, xml ) Need Help??


in reply to What does 'next if $hash{$elem}++;' mean?

  1. If a key doesn't exist in a hashref before it's used, it's created for you automatically. This is called autovivification.
  2. The postfix increment operator (++) will increment the value of the thing it's after, then return the old value.
  3. next will skip to the next iteration of the loop.

So, this code will skip to the next iteration of the loop if it's already seen that element. Just as if you'd read the code out loud. :-)


My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
  • Comment on Re: What does 'next if $hash{$elem}++;' mean?

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-24 02:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found