in reply to Re^2: foreach array manipulation
in thread foreach array manipulation

According to the people "who wrote the book on Perl" {grin}, there is indeed a "for" loop (what you would call "C style"), and a "foreach" loop (stolen mostly from C-shell). Even though the keywords of "for" and "foreach" are interchangeable, we call a foreach loop a foreach loop even when it's spelled "f-o-r".

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^4: foreach array manipulation
by blazar (Canon) on Jan 27, 2005 at 09:03 UTC
    Indeed it's a matter of user interface[*]. While
    foreach (my $i=0; i<10;$i++) { #...
    does indeed work thus illustrating the point I was talking about, C<for> is IMHO more intuitively appropriate. OTOH many perl programmers, and I for one, always use C<for> instead of C<foreach> for the perlish form. Which is one of the reasons why Larry chose to only have C<for> and a separate keyword, namely C<loop> for C-style loops, in Perl6, that is.

    [*]I'm thinking especially of that paragraph of Conways's in which he mentions another book on general UIs and talks about how if you have a door that must be pulled then you'd better put a handle on it and if must be pushed then something flat instead (sorry for the bad wording, could not come with anything better!)