in reply to perlgolf - bruteforceing an array

I'm wondering what you want to achieve! Do you want to get all possible combinations of 1-12 characters from your array stuffed into one array? So to give an example for 4 instead of 12 chars the resulting array should be this?
0,1,2,3, 01,02,03,10,12,13,20,21,23,30,31,32, 012,013,021,023,031,032, 102,103,120,123,130,132, 201,203,210,213,230,231, 301,302,310,312,320,321, 0123,0132,0213,0231,0312,0321, 1023,1032,1203,1230,1302,1320, 2013,2032,2103,2130,2301,2310, 3012,3021,3102,3120,3201,3210

Replies are listed 'Best First'.
Re: Re: perlgolf - bruteforceing an array
by denthijs (Acolyte) on May 12, 2003 at 14:20 UTC
    i want to try all the possible combinations to form dtmf sequences, .. so 000001 should be tried aswell.
    I'm testing my homemade phonesystem. (if it isnt broke, hit it again;)
      here is an explained version that gives you all combinations of a given length:
      # 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, '');
      HTH
        > how long should the numbers be?

        hehe , arbitrary :)

        the onleliner above got used in the dtmf-project
        (wich lost its glory after listening in to random dtmf sequences for about 2 hours;)
        but i did copy this one to my wiki, ...
        really handy to have laying around for future usage, thx :) (++;)
      How long should the numbers be?