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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.