in reply to For vs. Foreach
Care must be used if one is to say [...] they can be used "interchangeably".
The keywords can be used interchangeably. Any difference is purely stylistic.
Anyhow, I'm interested in why Perl has the two styles of "for" constructs
Three kinds of loops use for/foreach as far as I'm concerned:
The following iterative loops are optimised in one way or another:
"For" looks to behave differently depending on it's context: c-style vs. (er) foreach-style. If one uses c-style "for" loops you don't always get localised indexing variables as you seem to in "foreach" loops.
That's either unclear or wrong.
C-style for loops (whether written with for or foreach) don't localise the index variable simply because they don't accept an index variable.
However, you are free to declare variables in all three of its control expressions, and they will be scoped to the loop.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: For vs. Foreach
by Eimi Metamorphoumai (Deacon) on Feb 13, 2009 at 18:25 UTC | |
by ikegami (Patriarch) on Feb 13, 2009 at 19:04 UTC | |
by ack (Deacon) on Feb 13, 2009 at 19:16 UTC | |
by merlyn (Sage) on Feb 13, 2009 at 21:58 UTC | |
by BrowserUk (Patriarch) on Feb 14, 2009 at 04:01 UTC | |
by merlyn (Sage) on Feb 15, 2009 at 04:40 UTC | |
| |
by ikegami (Patriarch) on Feb 14, 2009 at 03:19 UTC | |
by Anonymous Monk on Feb 14, 2009 at 01:27 UTC |