[Israel.pm] BruteForcing function

Gaal Yahas gaal at forum2.org
Sun Dec 19 11:43:08 PST 2004

amit sides wrote:

> i wanna write function that do kind of bruteforcing..
> actually i want it to be a loop that shows every possible string by 
> [a-zA-Z0-9!_-] ( or anything else i'll give )/
> i´v tried to do a loop like
> $x="a"; while (1) { $x++; }
> but he shows only little latters...same with $x="A" and $x="1";
> if someone have an idea how to do that...or example i would like to 
> hear about it.

The term for what you want to do is to _enumerate_ over the range. You 
actually have two problems:

1. How to define what participates in this range, that is, what are the 
members of a set; and
2. How to visit each member of this set.

Enumerating over numbers is very easy, so if you map between members
of your set and integer numbers, you just need to do:

     for (0 .. $size_of_set - 1) {
         print number_to_member($_);

Fortunately, this is easy too. An array implicitly gives each member a 
numerical ID:

    @alphabet = ( 'a' .. 'z', 'A' .. 'Z', '0' .. '9', qw/!_-/ );
     for (0 .. $#alphabet) {
         print $alphabet[$_];

I've deliberately written this in long form because I am guessing you 
want to
do something other than just print each member. But for the above 
you can do simply this:

     print for @alphabet;

Or even (exploiting $,) just:

     print @alphabet;

Gaal Yahas <gaal at forum2.org>

More information about the Perl mailing list