in reply to Creating an Intermediate Perl Programming Curriculum

# foreach vs. for

Err, and the fact that they are exactly the same, just easier to read, depending on context. For example, these just don't flow through the brain as comfortably:

foreach (my $i=0; $i<10; $i++) { print "$i\n"; } for my $i (0..9) { print "$i\n"; }

But I know what you mean :)

cLive ;-)

Replies are listed 'Best First'.
Re: Re: Creating an Intermediate Perl Programming Curriculum
by Steve_p (Priest) on May 04, 2004 at 14:03 UTC

    Yes, that, and the ability to use for in a single statement like if and unless. For example:

    s/foo/nofoo for @array;

      You can use foreach in the exact same manner. They are synonyms. One can always be used in place of the other:

      $ perl -le 'print foreach 1, 2, 3' 1 2 3