sub make_iter_deeperer { my @AoA = @_; my @idx; my $i = -1; $idx[0] = -1; my @return; return sub { # depth first to produce "nested" result behavior # if we aren't at the end of AoA's elements, go to # the next, reseting the next elements' index if( $i < $#AoA ) { $idx[++$i]= -1; } LOOP: { if ( ++$idx[$i] > $#{@{$AoA[$i]}} ) { # if the index of the array i'm working in is # past the end of elements it means we need to # go up a level and clear the last return value pop @return; # we may have finished everything return if --$i < 0; # we may have finished more than one # so check again redo LOOP; } # if not, put the next element in the return value else { $return[$i] = $AoA[$i][$idx[$i]] } } return @return; } }