sub nested_foreach(&@) { my \$code = shift; my @indices = map { 0 } @_; # First set of indices is all zeroes my @sizes = map { scalar @\$_ } @_; # Cache array sizes (optional) my \$k; do { # Determine the array elements corresponding to the current set # of indices, and pass them to the closure: \$code->( map { \$_[\$_][\$indices[\$_]] } 0..\$#_ ); # Determine the next set of indices: for (\$k = \$#_; \$k >= 0; \$k--) { \$indices[\$k]++; if (\$indices[\$k] < \$sizes[\$k]) { last; } else { \$indices[\$k] = 0; } } # If \$k went out-of-bounds, there are no more valid iterations: } while (\$k >= 0); } my @a = ...; my @b = ...; my @c = ...; nested_foreach { say join ' ', @_ } \@a, \@b, \@c; ##```## sub nested_foreach(&@) { ... # see above } sub nForX(&@) { ... # see BrowserUK's post } # my @size = (500, 900); # my @size = (5, 5, 5, 5, 5, 5, 5, 5); my @size = (100, 4, 75, 23); my @AoA = map { [map { chr(\$_+64) x int(rand(10)) } 1 .. \$_] } @size; cmpthese -10, { iterative => sub { nested_foreach { join("", @_) } @AoA }, recursive => sub { nForX { join("", @_) } scalar @AoA, @AoA }, }; ##``````## s/iter iterative recursive iterative 1.86 -- -71% recursive 0.532 249% -- ```