Solved

Sort a Hash by Values routine?

Posted on 2001-06-19
2
236 Views
Last Modified: 2010-03-05
Sort a Hash by Values routine?

I'm writing a routine that interprets a Log file.
I'm saving the info about browsers in a hash and wish to show the Top-20 Browsers.
Like:    Mozilla/4.77   would be a log entry ,key, for netscape with perhaps the value of '50'.

My dumb problem:
Once I have a hash full of browser names as key, and how many hits for each in values.....what then?  What is an elegant way to find the top-20 ???

0
Comment
Question by:jgore
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
Comment Utility
for( (sort{ $hits{$b} <=> $hits{$a} }keys %hits)[0..19] ){
   print "$_=$hits{$_}\n";
}
0
 

Author Comment

by:jgore
Comment Utility
Thank you Ozo!
Worked like a charm!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now