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:
middle:1,last:2,beyond:3,

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.

Gilles.


In reply to Re^2: Auto-increment behaviour, is this normal and why? by Gilles Maisonneuve
in thread Auto-increment behaviour, is this normal and why? by Gilles Maisonneuve

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.