[EMAIL PROTECTED] wrote:
hi, perlers,
Is there a simple way to get the current index of an array in loop
statement? the procondition is you cannot get this information from
array element. For example
#!usr/bin/perl
my @arr = qw/a b c d e/;
for(@arr)
{
print "The No.?? element is $_\n";
}
There is no built-in method for determining the array index, because
for() and foreach() can iterate over things other than arrays.
So you have to keep track of the index yourself, either by iterating
over indexes like John showed, or keeping a separate variable:
my $i = 0;
for (@arr) {
print "The current element $_ is index $i\n";
}
continue {
$i++;
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>