http://www.perlmonks.org?node_id=439789


in reply to Foreach Loops

Updated: MapCar was a bad choice. This one is actually pretty cool:
use Algorithm::Loops 'NestedLoops'; my $iter = NestedLoops([\@arr, do {my $i=0; sub {[$i++]}} ]); while (my ($line, $x) = &$iter) { print "$x: $line\n"; }
Obviously, modifying $line isn't going to affect @arr, though. To do that, you'd have to do
use Algorithm::Loops 'NestedLoops'; my $iter = NestedLoops([[\(@arr)], do {my $i=0; sub {[$i++]}} ]); while (my ($line, $x) = &$iter) { for my $line ($$line) { print "$x: $line\n"; $line = 'X'; } } print "@arr\n";

Caution: Contents may have been coded under pressure.