Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Letter frequencies

by jeroenes (Priest)
on Nov 24, 2000 at 14:54 UTC ( [id://43207]=note: print w/replies, xml ) Need Help??


in reply to Letter frequencies

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.

Replies are listed 'Best First'.
Re: Re: Letter frequencies
by merlyn (Sage) on Nov 24, 2000 at 18:16 UTC
    tr/[ \t\n\r]//d;
    Are you literally wanting to remove [ and ] there? Remember, the operands to the tr operator are not character classes, so they don't need brackets.

    -- Randal L. Schwartz, Perl hacker

      Stomp on the head Yeah, of course. My excuses. I still have to get used to tr// . At first I wanted to tr/\s//d, but that was quite a disappointment! ;-).

      Thanks a lot!
      Jeroen

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://43207]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found