in reply to for ( ; ; ) vs for ( .. )

Iterating through a list is more efficient than looping over an index variable. This is because all the index keeping is done internally, where as the index variable solution has to do a lot of external operations (see. more CPU time).

Also you can't accidentally offset the index within the iterative code e.g

for(my $i = 0; $i < @ARGVl $i++) { if($ARGV[$i] =~ /meets some condition/) { # ack! the index has been offset $i += 2; } }
Compared to
for my $i (0 .. $#ARGV) { if($ARGV[$i] =~ /meets some condition/) { # doesn't matter as $i will be set again in the next iteration $i += 2; } }
This works both ways however as you may want to intentionally affect the offset, but you're possibly better off using a while loop in the first place.

Here's a quick benchmark for those in need of speed

use Benchmark qw(cmpthese); my @list = 1..10000; my $r; cmpthese(-10, { cstyle => sub { for(my $i = 0; $i < @list; $i++) { $r = $list[$i]; } }, perlstyle => sub { for my $i (@list) { $r = $i; } } }); __output__ Benchmark: running cstyle, perlstyle, each for at least 10 CPU seconds +... cstyle: 15 wallclock secs (10.57 usr + 0.05 sys = 10.62 CPU) @ 36 +.06/s (n=383) perlstyle: 15 wallclock secs (10.10 usr + 0.03 sys = 10.13 CPU) @ 86 +.77/s (n=879) Rate cstyle perlstyle cstyle 36.1/s -- -58% perlstyle 86.8/s 141% --

HTH

_________
broquaint

update: added benchmark because it's just so darned easy