#! perl -slw use strict; sub nFor(&@) { my $code = shift; die "First argument must be a code ref" unless ref( $code ) eq 'CODE'; my @limits = @_; my @indices = ( 0 ) x @limits; for( my $i = $#limits; $i >= 0; ) { $i = $#limits; $code->( @indices ), ++$indices[ $i ] while $indices[ $i ] < $limits[ $i ]; $i = $#limits; $indices[ $i ] = 0, ++$indices[ --$i ] while $i >= 0 and $indices[ $i ] == $limits[ $i ]; } } my @array = ( [ "a", "b", "c", ], [ "1", "2", "3", "4", ], [ "x", "y", ], ); nFor { print join '', map $array[ $_ ][ $_[ $_ ] ], 0 .. $#_ } map scalar @$_, @array; __END__ c:\test>nfor a1x a1y a2x a2y a3x a3y a4x a4y b1x b1y b2x b2y b3x b3y b4x b4y c1x c1y c2x c2y c3x c3y c4x c4y