If I have the following array in Perl:
@x = qw(a b c);
and I iterate over it with foreach
, then $_
will refer to the current element in the array:
foreach (@x) {
print;
}
will print:
abc
Is there a similar way to get the index of the current element, without manually updating a counter? Something such as:
foreach (@x) {
print $index;
}
where $index
is updated like $_
to yield the output:
012