in reply to Re^2: Quick easy question: What does $i mean?
in thread Quick easy question: What does $i mean?

What's wrong with the C-ish for loop?

It's harder to read. The index variable appears three times, and the upper bound is expressed as a logical expression, yet we think of the bound as a number. With a Perl-ish for loop, the index variable only appears once, and the bounds are expressed as numbers, just like in our mind.

To support my case, let me point out you made an error reading the bounds of the C-ish version:

for (0 .. $limit-1) { #... } ^^ was missing

Replies are listed 'Best First'.
Re^4: Quick easy question: What does $i mean?
by kwaping (Priest) on Sep 23, 2005 at 23:13 UTC
    ... Assuming that $limit wasn't already defined as "whatever - 1". You're thinking of $limit strictly in terms of "scalar @array", which was not what I was thinking when I wrote that little snippet. :)

      No, I was not assuming anything.

      for (my $i = 0; $i < $limit; ++$i)
      is not equivalent to
      for (0 .. $limit) { #... }
      but rather
      for (0 .. $limit-1) { #... }