in reply to getting iteration number inside iteration loop
You may get this behavior with List::MoreUtils. The following presents two such solutions. They are not very pretty, but they work.
First, you may generate an index array with [ 0 .. $#array ] and use each_arrayref/each_array to produce an iterator that loops over possibly many arrays at a time.
Second, you may use the documented behavior that the iterator returns the current index when given arguments ('index').use List::MoreUtils qw( each_arrayref ); my @a = qw( a b c d e ); my $each = each_arrayref( [ 0..$#a ], \@a ); while ( my ($idx, $v) = $each->() ) { print "$idx: $v\n"; }
use List::MoreUtils qw( each_array ); my @a = qw( a b c d e ); my $each = each_array( @a ); while ( my $v = $each->() ) { my $idx = $each->('index'); print "$idx: $v\n"; }
|
---|
In Section
Seekers of Perl Wisdom