(I tried posting this as a comment to brian's answer, but couldn't get the formatting right.)
You have to be careful to avoid nested uses of each
. each
works on a "global" iterator on the array. When it reaches the end, it returns false and then resets the position to the beginning. Thus following code results in an infinite loop.
Thanks to RJBS for his talk at YAPC::NA where he pointed out the global nature of the built-in iterator.
use strict;
use warnings;
my @array = 'A' .. 'J' ;
while ( my ($index, $value) = each @array){
print "printing ($index, $value) from outer loop\n";
while ( my ($index_in, $value_in) = each @array){
print "printing ($index_in, $value_in) from inner loop\n";
}
}