Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Fellow monks,
I had a task to do in Perl yesterday, and it was a simple one.

I had a 25,000-word cracking dictionary file I downloaded, and I wanted to split it up by word length (My motivation is to do with cryptic crosswords).

I just wanted to put all the 10-letter words into a file called 10.words and so on.

So I wrote it the following very dumb way. I knew it was dumb, but I knew it would work.

while(<>){ chomp($_); $len = length($_); open(OUTPUT,">>${len}.words"); print OUTPUT "$_\n"; close(OUTPUT); }

And you've got to admit, it was quick to code.

Then, tortured by how dumb it was, I came back to the problem and figured something else out -- once I'd checked that I could have an array called "@1" and an array called "@2" and so on -- for a while I was convinced that was illegal, though I can't say why.

This is the better way:

while(<>){ chomp($_); $len = length($_); if($longest < $len){ $longest = $len; } push(@{$len},$_); } $"="\n"; for($x=1;$x<=$longest;$x++){ open(WORDS,">$x.txt") || die "$!"; print WORDS "@${x}"; close(WORDS); }

Who wants to guess how much smarter the second was than the first?

The first way took 212 seconds (on a computer with a 333MHz chip) and the second took two seconds.

I just thought you might be interested, or have comments.

($_='jjjuuusssttt annootthhrer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;

In reply to Extreme Example of TMTOWTDI by Cody Pendant

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (3)
    As of 2020-02-21 19:49 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (97 votes). Check out past polls.