in reply to which loop is better

There's no real difference in speed. The difference is in structure - for allows you to initialize, increment, and test, while foreach just increments through a list of items. The following are equivalent:
foreach (@arr[0..2]) { $_++; } for (my $i = 0; $i <= 2; $i++) { @arr[$i]++; }
The latter offers more control, but isn't necessary for most looping purposes.

Replies are listed 'Best First'.
Re^2: which loop is better
by Joost (Canon) on Oct 04, 2004 at 10:54 UTC
      Err, I don't think what you say is very clear. For and foreach are synonyms, but either of them can be used to delcare two completely different loop contructs, ie the following two lines are identical to each other
      for (expr; expr; expr) block foreach (expr; expr; expr) block
      while the following two are identical to each other, but not to the first two lines above
      for (list) block foreach (list) block

      Dave.