use Algorithm::Loops 'NestedLoops'; my $iter = NestedLoops([\@arr, do {my $i=0; sub {[$i++]}} ]); while (my ($line, $x) = &$iter) { print "$x: $line\n"; }