Just for fun, I want to post a script that does without the loops. So with map/ no while or for. What do you think?

undef $/; #just read the whole STDIN in once: $_=<>; tr/ \t\n\r//d; #remove all spaces map{ $count{ $_ }++;} split //; #the actual count foreach $key (sort(keys %count)) #OK, one loop to print { print "$key\t$count{$key}\n"; $total += $count{$key}; } print "Total\t$total\n";
Well, I tested it, and this is the tail of the output, run with time perl .... (too lazy for benchmarking):
ó 18 ô 93 õ 12 ö 16 ÷ 12 ø 25 ù 2 ú 3 û 1 ü 2 ý 6 þ 2 ÿ 6 0.26user 0.05system 0:00.62elapsed 49%CPU (0avgtext+0avgdata 0maxresid +ent)k 0inputs+0outputs (275major+797minor)pagefaults 0swaps [jeroen@rulffk] time: 11:49:31 1 jobs wd: ~ (12.011Mb) $ ls -l test.txt -rw-rw-r-- 1 jeroen jeroen 150271 Aug 17 11:40 test.txt [jeroen@rulffk] time: 11:50:04 1 jobs wd: ~ (12.011Mb)
And it looks to me like a 150kb file that has been counted in just 0.62 s. Looks pretty impressive to me. I don't consider myself to be a decent perl coder, so I would appreciate any comments to this....

Have fun,

Jeroen

I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)

PP: I just took a .txt file, but it wasn't a text-file after all. Maybe some word (ugh!) document or whatever.

Update Removed square brackets from tr///d, as pointed out by merlyn. And changed a funny typo (s/while/whole/). Justifies my remark on decent coding, I'm afraid :-<. Another update: added the total number.


In reply to Re: Letter frequencies by jeroenes
in thread Letter frequencies by Leitchn

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.