Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^3: list item comparison

by dasgar (Curate)
on Jul 13, 2012 at 03:28 UTC ( #981547=note: print w/ replies, xml ) Need Help??

in reply to Re^2: list item comparison
in thread list item comparison

When you increment a the value for a key which doesn't exist, the key springs into existence.

Didn't know that. Thanks for the info.

However, I'd like to point out one thing. Back when I was first learning Perl, your one line for loop code would have had me thoroughly confused. Based on the original question asked by the OP, I was left with the impression that the OP was a bit of a beginner. In that case, my foreach loop would probably be somewhat easier to follow along and understand.

Plus, I personally find that my foreach loop more closely matches how I would think through the task, which in turn would mean easier code for me to personally maintain. Just a personal preference.

Anyways, thanks for the tip!

Comment on Re^3: list item comparison
Replies are listed 'Best First'.
Re^4: list item comparison
by nitin1704 (Sexton) on Jul 13, 2012 at 04:49 UTC

    Yes, it's easier to understand the way you presented it. The one line syntax (statement modifier form) is fairly common in Perl though, and it's great if beginners can pick it up. @OP: This syntax can be used with both loops and conditions. E.g:

    if($cond){ doSomething(); }

    can be replaced by:

    doSomething() if $cond;

    The only catch is that you can execute only one statement per iteration/condition with this syntax. Happy coding!

      The only catch is that you can execute only one statement per iteration/condition with this syntax.

      That's where do blocks come in handy :-)

      knoppix@Microknoppix:~$ perl -E ' > $sayIt = 1; > do { > say q{Hello}; > say q{World}; > } if $sayIt;' Hello World knoppix@Microknoppix:~$

      I hope this is of interest.



        There's always something to learn! Thanks John. I'm a beginner too by the way, started learning Perl a couple of months ago.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2015-11-30 02:38 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (756 votes), past polls