After reading ``This turned out not to be the disaster I expected'', I got to thinking. Instead of generating all the strings of a given length and evaling them, could I generate a list of all the valid perl of a given length?
I do, of course, want to include punctuation and numbers (perl isn't all that exciting without them, mind-bending japhs aside), so the magic string autoincrement won't work for me. The code I ended up with is below.
#!/usr/bin/perl -w use strict; # redirect stderr to /dev/null when run, if you'd # like output of any use. my $chars= "abcdefghijklmnopqrstuvwxyz0123456789 `~!@#\$%^&*()-_=+[{]}\\|;:'\",<. +>/?"; my $f = substr($chars, 0, 1); my $l = substr($chars, -1, 1); my $s = ""; while(length($s) < 5) { my @tmp; my $x; if (system("perl", "-ce", $s) == 0) { print $s . "\n"; } if ($s eq "") { $s = $f; next; } elsif ($s eq ($l x length($s))) { $s = $f x (length($s) + 1); next; } @tmp = split(//, $s); $x = @tmp - 1; while (1) { # loop through the string, starting at the end if ($tmp[$x] eq $l) { # Is it the last char? $tmp[$x] = $f; # If so, make it the first $x--; # And move on to the next one } else { # If not, just increment it. $tmp[$x] = substr($chars, index($chars, $tmp[$x]) + 1, 1); last; } } $s = join('', @tmp); }
There's a big problem with this code, though. It's slow. Like, really slow. I think most of the slowdown comes from having to spawn perl every iteration of the loop[0]. I'm confident there's a better way to do the syntax checking, but I don't know what it is. So, due to that limitation the results aren't very exciting.
I think a more intelligent algorithm, something that's aware of perl's syntax, could be used to get better results faster. At the very least, random generation would be more interesting on the longer strings (if the length is 8, every string length 7 needs to be generated before the front character changes...)
-- [0] The rest is from my poor code :)In reply to Brute force perl by jagh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |