# you call it with the length, a refernce to your # character Array and a string thats already done sub combine { my($length,$chars,$ready)= @_; my($char); # First test whether or not we have to add chars if ($length) { # if so we iterate through the chars foreach $char (@$chars) { # and call combine # recursively by decrementing the # level and adding one char to # the chars we already have combine ($length-1, $chars, $ready.$char); } } else { # if there is nothing left to do, # we simply print the result. # we could also push it to a global # array for later processing print $ready,"\n"; } } # this is the first call @chars=qw(0 1 2 3 4 5 6 7 8 9 # *); combine (2, \@chars, '');