Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Sort hashes

Reply
Thread Tools

Sort hashes

 
 
Raj
Guest
Posts: n/a
 
      02-15-2005
Hi,

I have a need to sort a hash of hashes! I know it already sounds
complicated, but its the only way I can think of holding the results of the
analysis of some data.

The hash has entries like:

$ips{"F1234"}{"telephone"} = "02088887777"
$ips{"F1234"}{"charge"} = 1000
$ips{"F1234"}{"err_type"} = 0

$ips{"F6638"}{"telephone"} = "02077776666"
$ips{"F6638"}{"charge"} = 500
$ips{"F6638"}{"err_type"} = 2

$ips{"B7877"}{"telephone"} = "02077756666"
$ips{"B7877"}{"charge"} = 445
$ips{"B7877"}{"err_type"} = 2

I need to sort it using the key "err_type"....is this easy? I've never
understood sorting in Perl and so would appreciate any help you can give me.
"err_type" is always a number, if that's relevant!

Thanks in advance,

- raj


 
Reply With Quote
 
 
 
 
phaylon
Guest
Posts: n/a
 
      02-15-2005
Raj wrote:

> $ips{"F1234"}{"telephone"} = "02088887777" $ips{"F1234"}{"charge"} = 1000
> $ips{"F1234"}{"err_type"} = 0
>
> $ips{"F6638"}{"telephone"} = "02077776666" $ips{"F6638"}{"charge"} = 500
> $ips{"F6638"}{"err_type"} = 2
> ...
> I need to sort it using the key "err_type"....is this easy? I've never
> understood sorting in Perl and so would appreciate any help you can give
> me. "err_type" is always a number, if that's relevant!


How about:

sub errtype { $ips{ $a }{err_type} <=> $ips{ $b }{err_type} }

for( sort errtype keys %ips ) {
print "$_\n";
}

This gives the keys of your (level 0) hashes to sort. Sort uses a function
here, gives it $a and $b (the two values that get compared to tell which
is higher). These two vars (two keys of your level 0 hash) we use, well,
as keys for your level 0 hash, but we don't compare (maybe you lookup the
<=> operator) the keys (F6683) but the values of the err_type field in the
hashref saved as value to your level 0 hash.

g,phay

--
http://www.dunkelheit.at/
bellum omnium pater.

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-15-2005
Raj wrote:
> I have a need to sort a hash of hashes!


perldoc -q "sort a hash"

> I've never understood sorting in Perl


perldoc -f sort

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
On Hashes - How the hashes printing works? Neela megha shyam Chivukula Ruby 4 05-28-2009 10:56 AM
How to make an array of hashes to a single array with all thevalues of these hashes ? kazaam Ruby 12 09-13-2007 01:30 PM
using hashes as keys in hashes Steven Arnold Ruby 3 11-23-2005 03:25 PM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57