CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Sorting the output of a hash array by value

 



blondem
New User

Aug 22, 2002, 7:45 AM

Post #1 of 5 (886 views)
Sorting the output of a hash array by value Can't Post

Hello, I'm fairly new to Perl programming and am stumped on a problem that does not seem to be addressed in any of the books I have.

I have a sub routine that parses e-mail for external port hits on our firewall (this part works). It populates a hash array called %port_fail. %Port_fail looks something like this:

my %port_fail=(
"47992" => 4,
"54777" => 32,
"3453" => 1,
"6548" => 23
#etc ....
);

I want to output these keys and values sorted by the value (ex:)

54777 = 32
6548 = 23
47992 = 4
3453 = 1

Is there a way to use the sort function (which seems easy to use with a list array) to sort hash arrays by value and, if not, how would one go about doing this.

Any help would be greatly appreciated!


davorg
Thaumaturge / Moderator

Aug 22, 2002, 9:41 AM

Post #2 of 5 (882 views)
Re: [blondem] Sorting the output of a hash array by value [In reply to] Can't Post

You can't sort a hash. Hashes are by definition unsorted. You can, however, display the contents of a hash in a sorted order - and I suspect that's what you want.

It looks like you want the output sorted in descending numeric order. This is how to do it:

Code
foreach (sort { $port_fail{$b} <=> $port_fail{$a} } keys %port_fail) { 
print "$_ = $port_fail{$_}\n";
}


See the documentation on sort for more details on how it works.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


blondem
New User

Aug 22, 2002, 9:46 AM

Post #3 of 5 (881 views)
Re: [davorg] Sorting the output of a hash array by value [In reply to] Can't Post

Thanks for your response. I'd finally figured it out, but your method is much less bulky. I think I will use it.

Thanks again!


Paul
Enthusiast

Aug 22, 2002, 11:01 AM

Post #4 of 5 (879 views)
Re: [davorg] Sorting the output of a hash array by value [In reply to] Can't Post

>>
You can't sort a hash. Hashes are by definition unsorted. You can, however, display the contents of a hash in a sorted order
<<

Erm what is the difference between sorting and displaying in a sorted order?

Hashes do sort themselves.


Code
my %h = ( 5 => 'a', 3 => 'c', 4 => 'b' ); 

print %h;



davorg
Thaumaturge / Moderator

Aug 22, 2002, 12:23 PM

Post #5 of 5 (877 views)
Re: [RedRum] Sorting the output of a hash array by value [In reply to] Can't Post


In Reply To
>>
You can't sort a hash. Hashes are by definition unsorted. You can, however, display the contents of a hash in a sorted order
<<

Erm what is the difference between sorting and displaying in a sorted order?


To my mind the phrase "sorting a hash" implies that the data in the hash is stored in a sored order (as it is in an array). That's never true. The order that the hash elements are stored in is defined by a hashing algorithm. That's where the name comes from.


In Reply To
Hashes do sort themselves.


Code
my %h = ( 5 => 'a', 3 => 'c', 4 => 'b' ); 

print %h;


You got lucky. Sometimes the hashing algorithm does seem to sort the array. But that can't be relied on. Try this:


Code
#! /usr/bin/perl -w 

use strict;

my %h;

for (1 .. 20) {
$h{$_} = 'x';
print %h, "\n";
}

Using Perl 5.6.1 on RedHat Linux 7.3, the hashing algorithm no longer gives a sorted hash once the key value goes beyond 9. But this behaviour is also dependent on Perl version and operating system.

See also the answers to How do I sort a hash (optionally by value instead of key)? and How can I always keep my hash sorted? in perlfaq4

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives