For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > get maximum occurances of word in line









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author get maximum occurances of word in line
Ram Prasad

2006-11-18, 3:57 am

I have a line like

"abc ab ab abc abc w1 w2 w4"
How do I get which word repeats most times in this line

I am already doing "split into words , foreach word count occurrances ,
sort on occurrances "
Can there be something more efficient


Thanks
Ram


PS:
Note to Spammers: Go ahead , send me spam
ram@netcore.co.in
http://ecm.netcore.co.in/spamtrap.html

DJ Stunks

2006-11-18, 6:56 pm

Ram Pra wrote:
> I have a line like
>
> "abc ab ab abc abc w1 w2 w4"
> How do I get which word repeats most times in this line
>
> I am already doing "split into words , foreach word count occurrances ,
> sort on occurrances "
> Can there be something more efficient


split into words;
foreach word increment counter for that word (use a hash: 'word' =>
$count);
sort hash by descending value ( sort { $hash{$b} <=> $hash{$a} } keys
%hash );

-jp

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com