Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Sorting hash keys by sub hash values

by ZlR (Chaplain)
on Mar 31, 2010 at 13:26 UTC ( #832043=perlquestion: print w/ replies, xml ) Need Help??
ZlR has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I know i'm being a little lazy here, but i'm still working on cleaning my HoHoH script and it makes my head hurt !

I now have the following hash :

'card' => { 'mac1' => { 'fa' => ' +9A', 'port' => + '0' }, 'mac2' => { 'fa' => ' +8A', 'port' => + '0' } }

I want to sort the keys according to val, ie get : (item2, item1) since 8A < 9A

I've done that before but can find it back, i'm sure you have a gazillion ways of doing it :)

Comment on Sorting hash keys by sub hash values
Download Code
Re: Sorting hash keys by sub hash values
by moritz (Cardinal) on Mar 31, 2010 at 13:28 UTC
    See perlfaq4 for answers to very similar problems.
    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Sorting hash keys by sub hash values
by Corion (Pope) on Mar 31, 2010 at 13:28 UTC
      ok, ok, so it wasn't so difficult to find a way to do it :
      my $ref = $hash->{'card'} ; my @keys = sort { hex $ref->{$a}{'fa'} <=> hex $ref->{$b}{'fa'} } key +s % { $ref } ;
      Thanks !

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://832043]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (12)
As of 2014-09-23 18:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (237 votes), past polls