in reply to Re: Risks of equivalence between FOR and FOREACH
in thread Risks of equivalence between FOR and FOREACH
My personal convention is to write counting loops and C-style loops using for, and iterating loops using foreach. That way, the extra cost of flattening the list is not hidden. However, Perl will gladly accept both for and foreach in every case.
And many people share your pov, while I'm of the "three chars are easier to type no matter what" school of thought - I also think we've "argued" about these issues in the past, but I'm not really sure. However to contribute with something concrete to this thread, it's also interesting if not properly relevant, to note that in Perl 6 the possible/alleged ambiguity will be removed, with C-style for loops being renamed to loop, and for being the preferred kind of loop for most situations, while foreach will be phased out:
pugs> sub postfix:<!> (Int $x) { [*] 1..$x } undef pugs> say $_! for =<>; 3 6 4 24 7 5040 10 3628800 undef pugs>
|
|---|