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

As it allows you to change the values of the array with foreach loop, which is faster than others (for or while).
Huh?!? Maybe I misunderstood the sense of your words, but C<for> and C<foreach> are just synonims. Or were you referring to C-style C<for>'s (C<while>'s in disguise)?!?

Replies are listed 'Best First'.
Re^3: foreach array manipulation
by merlyn (Sage) on Jan 26, 2005 at 12:54 UTC
    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.

      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!)

Re^3: foreach array manipulation
by Hena (Friar) on Jan 27, 2005 at 06:29 UTC
    Yup, i do call '($i=0;$i<$j;$i++) {}' a for loop and 'foreach (@list) {}' a foreach loop. I don't use 'for (@list) {}' inorder to avoid confusion like this :).

    But thanks for merlyn for info on what others think.