Re: the way to get current index of an array





zhao_bingfeng@xxxxxxxxxxxxx wrote:
hi, perlers,

Howdy,

Is there a simple way to get the current index of an array in loop

yes

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";
}

for my $idx (0 .. $#arr) {
print "The no $idx element is $arr{$_}\n";
}

my $idx = 0;
for my $item(@arr) {
print "The no $idx element is $item\n";
$idx++;
}

There are other more magical ways but those will probably be best for you :)

The first way not only has less code but is faster.

HTH :)
.



Relevant Pages