It's really quite simple - for and foreach are the
same. When you don't care about the value of the current index, don't use it:
@array = ('a'..'z');
print $_,$/ for @array;
When you do care about the value of the current index, do
use it:
printf "%02d: %s\n", $_+1, $array[$_] for 0..$#array;
When in doubt, check with Deparse.pm:
$ perl -MO=Deparse foo.pl
@array = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z');
foreach $_ (@array) {
print $_, $/;
}
foreach $_ (0 .. $#array) {
printf "%02d: %s\n", $_ + 1, $array[$_];
}
foo.pl syntax OK
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
|