I was taught that for structure was like this:
(starting value, ending value, increment).
But I infer from your response that it must be:
(starting value, continue while this is true, increment)? | [reply] |
It's for (EXPR1; EXPR2; EXPR3) {...}. Note that there are semi-colons between the expressions, not commas.
The first expression is run before entering the loop. Before each iteration the second expression is run; if false, the loop terminates. After each iteration, the third expression is run. For further details, see man perlsyn.
| [reply] [d/l] [select] |
Note that there are semi-colons between the expressions, not commas.
In this case, the OP has constructed a foreach loop iterating $_ over a list of three elements: $i=500, $i=900 , $i += 100, which evaluates to 500, 900, 1000 and $i set to 1000 for ALL iterations of the loop.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
| [reply] [d/l] [select] |
Ugh the carelessness.. Thank you, I just needed to realize semi colons were to be used.
| [reply] |
Perls three-argument for loop is exactly like in C: initializer, test, and counting. See also http://en.wikipedia.org/wiki/For_loop#Three-expression_for_loops. Other languages may implement for loops differently, notably BASIC and its derivates often have constructs like for i=1 to 100 step 3 (counting up from 1 with increments to 3 until 100 is reached or exceeded) or even for i=100 downto 1 step 3 (counting down in steps of 3, starting at 100, stopping when 1 is reached or exceeded). Other languages have even stranger for loops, see the wikipedia article.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
| [reply] [d/l] [select] |
for (my $i = 0; $i < $n; ++$i) {
...
}
The above is equivalent to the following, except $i is scoped to the loop.
my $i = 0;
while ($i < $n) {
...
} continue {
++$i
}
| [reply] [d/l] [select] |