2009/10/25 Michael Alipio <daem0n...@yahoo.com>: > Hi, > > I'm trying to write a word list generator which can > generate all possible combinations of n characters, within n > set of characters. > > > So far, this is what I have come up. The only input is the > lenght of the password the user wants. > > my @set = qw(a b c d e f g h i j k l m n o p q r s t u v w > x y z); > my $len = scalar @set; > my $char1; > my $char2; > my $char3; > my $char4; > my $char5; > my $char6; > my $char7; > my $char8; > > if ($pwlen == 8){ > > for ($char1=0;$char1<$len;$char1++){ > for ($char2=0;$char2<$len;$char2++){ > for ($char3=0;$char3<$len;$char3++){ > > ... upto > > for ($char8=0;$char8<$len;$char8++){ > > print > "$set[$char1]$set[$char2]$set[$char3]$set[$char4]$set[$char5]$set[$char6]set[$char7]$set[$char8]\n"; > > }}}}}}}} > > } elseif ( $pwlen == 7){ > > for ($char2=0;$char2<$len;$char1++){ > for ($char3=0;$char3<$len;$char2++){ > for ($char4=0;$char4<$len;$char3++){ > > ... upto > > for ($char8=0;$char8<$len;$char8++){ > > print > "$set[$char2]$set[$char3]$set[$char4]$set[$char5]$set[$char6]$set[$char7]set[$char8]\n"; > > }}}}}}} > > } > > > The problem with the code above is that the length of words > is hard coded. Only 8 maximum. I'm looking for ways on how > to make my code flexible (length can be whatever the user > wants ). > > My code is limited to 8 chars maximum length plus if I want > to increase it, I have to add another set of for loops plus > another variable to use, e.g., $char9, char10.. and so > on... > > > Any idea?
What about keeping the characters in an array @char so you will have $char[0], $char[1] etc. a nd the loops could be replaced by a recursive function call. Something like this: do_something_for_char($k) sub do_something_for_char { my ($k) = @_; return if $k >= $n; do_something_for_char($n+1); } Gabor -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/