Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Auto-increment behaviour, is this normal and why?

by kennethk (Abbot)
on Oct 18, 2013 at 18:56 UTC ( #1058797=note: print w/replies, xml ) Need Help??

in reply to Auto-increment behaviour, is this normal and why?

The ++ is after variable, so it increments after you test it; however, the print is a separate statement. If you used Compound Statements instead of Statement Modifiers, your code would better reflect the actual flow and would look like:
$i=0; if ($i++) { print 'first=' , $i, ', '; } if ($i++) { print 'middle=' , $i, ', '; } if ($i++) { print 'last=' , $i, ', '; } if ($i++) { print 'beyond=' , $i, ', '; }
For what you want, you probably mean:
$i=0; if ($i++) { print 'first=' , $i - 1, ', '; } if ($i++) { print 'middle=' , $i - 1, ', '; } if ($i++) { print 'last=' , $i - 1, ', '; } if ($i++) { print 'beyond=' , $i - 1, ', '; }
which is admittedly a little awkward. More natural would be to post-increment after your print, except you clearly intend the incrementation to happen independently of whether the print happens. Perhaps
$i=0; print 'first=' , $i, ', ' if $i; $i++; print 'middle=', $i, ', ' if $i; $i++; print 'last=' , $i, ', ' if $i; $i++; print 'beyond=', $i if $i; $i++;
my $i=0; outputter('first', $i++); outputter('middle', $i++); outputter('last', $i++); outputter('beyond', $i++); sub outputter { my ($txt, $i) = @_; print "$txt=$i, " if $i; }

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: Auto-increment behaviour, is this normal and why?
by Gilles Maisonneuve (Initiate) on Oct 19, 2013 at 03:33 UTC
    Dear Kenneth,

    Thank you for your answer.

    So if understand what you expressed by "the print is a separate statement", the process is executing the following steps:

    1. it tests the if condition
    2. it incréments
    3. it prints only if (1)

    Thus because in the 1st case the if was false, the increment is done and the print not.
    So at instruction 2 $i is already 1 and incremented to 2 before print, not after on the contrary of what I thought.
    Then, as you shown me, when I write:
    $i=0; for $lbl (qw/first middle last beyond/) {print $lbl.':',$i-1,',' if $i++}
    I get what I expect:

    All right, I was misunderstanding the time sequence when the increment applies with the if modifier (I still prefer it, it's shorter, I just need to learn to control it).

    Thank you again very much.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1058797]
[LanX]: ;p
[marto]: LanX I'd rather commit sudoku :P
[marto]: trench humour folks, feeling rough.
[marto]: there is never a night when they sleep all the way through, but last night was something else
[marto]: gave in at 3:45 ish and let Charlie watch videos about spitfires, the battle of Britain. He's plane crazy
[1nickt]: marto soothing!
[marto]: Hiromi makes an appearance, let's hope Jools doesn't ruin it by joining in on the old Joanna
[1nickt]: karlgoethebier What is the issue with the semic-colon after the ellipsis? It's documented as proper syntax ...;

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2017-11-18 18:51 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (277 votes). Check out past polls.