in reply to Index in For loop
No special variable is available. Here are some options:
my @array = (1,2,3,4,5); my $count = 0; for (@array) { print "index: ".$count++." contains $_\n"; } for my $index (0..$#array) { print "index: $index contains $array[$index]\n"; } # if you prefer thinking in terms of lines (like files) for my $line (1..@array) { print "line: $line contains $array[$line-1]\n"; } # reading from a filehandle $count = 0; while (<DATA>) { chomp; next unless $_; $count++; print "line: $count contains $_\n"; } # kinda golfing print "index: ".$index++." contains $_\n" for @array; __DATA__ 1 2 3 4 5
cheers
tachyon
|
|---|