in reply to Re: perlgolf - bruteforceing an array
in thread perlgolf - bruteforceing an array

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;)
  • Comment on Re: Re: perlgolf - bruteforceing an array

Replies are listed 'Best First'.
Re: Re: Re: perlgolf - bruteforceing an array
by Skeeve (Parson) on May 13, 2003 at 10:39 UTC
    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 :) (++;)
Re: Re: Re: perlgolf - bruteforceing an array
by Skeeve (Parson) on May 13, 2003 at 07:50 UTC
    How long should the numbers be?