I was trying to make a list of all the strings that would match something like [0-9a-zA-Z]{3}, to get from 000 to ZZZ, and all separated by a newline. At first I was making nested loops, but then thought that, if I wanted something of length 7, I didn't want to type another loop with another variable, etc. However I hate recursion, even though I'm sure that would have been the smallest, and probabbly most efficient result. I ended up with this little beauty, which doesn't deobfuscate much with perltidy or -MO=Deparse.

I also know I've used every punctuation variable I could, and broken all the rules, haha. It's not as compressed/golfed as it can be, it's just...a mess.
Code:
#!/usr/bin/perl sub generate_list{$_=q!$;=@_[0];$;--;$_='(0...9,"a"..."z","A"..."Z")'; +for($i=0;$i<=$;;$i++){$*.="\@t$i=$_;";$@.="for(\$c$i=0;\$c$i<=\$#t$i; +\$c$i++){";}$.=$*.$@;$:='print';for($,=0;$,<=$;;$,++){$:.="\$t$,\[\$c +$,\].";}$:.='$/;'.'}'x($;+1);return$..$:;!;eval eval}generate_list 2;
Hope you like it :)
(I know it's one of the least efficient ways of doing this, haha)

Replies are listed 'Best First'.
Re: Generating lists from a charset
by TedPride (Priest) on Dec 24, 2005 at 00:38 UTC
    use strict; use warnings; our @c = (0..9,'a'..'z','A'..'Z'); build(7); sub build { my ($n, $s) = @_; if (!--$n) { print "$s$_\n" for @c; return; } build($n, "$s$_") for @c; }
      :O
      Impressive!
      Recursion is always better, haha.
Re: Generating lists from a charset
by QM (Parson) on Dec 23, 2005 at 20:07 UTC
    glob is your friend:
    my $charset = '{' . join(',', '0'..'9', 'A'..'Z', 'a'..'z') . '}'; my @charlist = glob( $charset x 3 );
    However, that's 238,328 elements. You might want to use glob in a scalar context, such as:
    while my $list ( glob( $charset x 3 ) ) { do_something_with($list); }

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      However, that's 238,328 elements. You might want to use glob in a scalar context

      Unfortunately, using glob in a scalar context does not prevent it from pre-generating the entire list before giving you the first of them, so using it in a scalar context makes little or no difference to either the memory consumed, or the performance.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        You're right. Perhaps it's time for glob to be updated?

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

      See, I knew I was missing something, thanks :)
      The memory consumed is pretty big though, startup on my machine takes just under 6 seconds :O
Re: Generating lists from a charset
by radiantmatrix (Parson) on Dec 23, 2005 at 18:57 UTC

    I am working on solving a similar problem with generation of rainbow tables (tables of strings and their hashes). I didn't want recursion because I needed restartability and arbitrary length.

    I have something that works, albiet it needs a bit more work:

    Basically, you could then solve your problem with:

    use RainbowTable; # I know we don't need a hash, but the module requires one. my $generator = RainbowTable->new( hash => 'MD5', chars => [ ('A'..'Z'), ('a'..'z'), (0..9) ], minlength => 3, maxlength => 3, ); while ($generator->next) { print $generator->string, "\n"; }

    Yeah, I know this is offtopic in an Obfu node, but I thought this might actually help you out. If you make useful modifications, I'd love to see them; my code's MIT licensed, though I haven't pasted that in yet -- basically, use to your heart's content, but give me credit.

    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law
Re: Generating lists from a charset
by jdalbec (Deacon) on Dec 24, 2005 at 22:06 UTC
    #! /usr/bin/perl use strict; use warnings; chop $"; my @c = ('0'..'9', 'a'..'z', 'A'..'Z'); sub generate_list { for(my @ii = (0) x ($_[0] + 1); $ii[0] == 0; do { for(my $i = $#ii; ++$ii[$i] == @c; $ii[$i--] = 0) {} } ) { print "@c[@ii[1..$#ii]]\n"; } } generate_list 2;
Re: Generating lists from a charset
by Anonymous Monk on Dec 23, 2005 at 15:56 UTC
    Sweet monkey Jesus on a pogo stick! I've not seen a thing that ugly since I ran into the south end of a northbound moose. Very thoroughly obfuscated, though. Two thumbs way up!

      Not obfuscated, recursive and aimed at a slightly different target, but somewhat related is this thread of mine, with lots of following discussion that the OP may still find to be useful. Feel free to exand (or compress ;-) on it, just like Tanktalus did.