Solved

Sort a Hash by Values routine?

Posted on 2001-06-19
2
238 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
ID: 6209123
for( (sort{ $hits{$b} <=> $hits{$a} }keys %hits)[0..19] ){
   print "$_=$hits{$_}\n";
}
0
 

Author Comment

by:jgore
ID: 6209877
Thank you Ozo!
Worked like a charm!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

867 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

21 Experts available now in Live!

Get 1:1 Help Now