Well, one of the great things about Perl is that there's
always a way of cheating. In this case, the cheat is to
note that s/// returns the number of replacements (and
gets rid of the letters we've already looked at).
So:
{
local $/ = undef; # Read everything in one go
$_ = lc(<STDIN>);
s/[^a-z]//g; # Get rid of nonalphabetic character
+s
for my $letter ('a'..'z')
{
my $count = s/$letter//g; # Magic!
print "$letter = $count\n";
}
}
Now, that's many times faster than your original, and a
fair bit shorter. I suspect it can be made faster still,
though (is the slowdown produced by using s/// offset by
the speedup on the next iteration, for instance?)
perl /home/ahunter/original.pl 154.02s user 2.39s system 88% cpu 2:56.29 total
perl /home/ahunter/flib.pl < xlib.ps 4.48s user 0.14s system 93% cpu 4.942 total
Well, that *was* impressive...
-- Andrew
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.