Is there any way that number of elements iterated in an for loop can be traced in perl: Like using special variables:
@arrayElements = (2,3,4,5,6,7,67);
foreach (@arrayElements) {
# Do something
# Want to know how may elements been iterated after
# some n number of elements processed without using another variable.
}
In perl5 v12 and later, you can use the each
iterator:
while(my($index, $element) = each @array_elements) {
...;
}
However, the more portable solution is to iterate over the indices, and manually access the element, as shown by ysth.
In any case, the number of elements that were visited (including the current element) is $index + 1
.