Even when you want to loop over the indexes, you can still use a foreach-style loop:
foreach my $i (0 .. $#mylist) { say "first element" if ( $i == 0 ); }
foreach-style loops are generally a better idea that C-style for loops. They're clearer; more declarative. It's harder to introduce an off-by-one error, and easier to spot a mistake.
In reply to Re^2: Determine first element in foreach
by tobyink
in thread Determine first element in foreach
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |