Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Sorting a Hash of Hashes

by HuckinFappy (Pilgrim)
on Jul 09, 2006 at 03:01 UTC ( [id://559975]=note: print w/replies, xml ) Need Help??


in reply to Sorting a Hash of Hashes

Let's look at what you're doing here (slightly reformatted):
%foo = ( 11 =>{ 4 => 'Four' }, 33 =>{ 1 => 'One' }, 2 =>{ 2 => 'Two' } ); my $ref_HoH = \%foo; for my $k ( sort { %{ $ref_HoH{$a} } <=> %{ $ref_HoH{$b} } } keys %$ref_HoH ) { ... }
Your sort routine is sorting on the values of each hash entry. That means you're sorting by memory address, since the value of each entry is an anonymous hash, so you are looking at the addresses of each anonymous hash.

All you really need is to simplify the sort (Hey, make it simple, what good news!)

use strict; use warnings; my %foo = ( 11 => { '4'=>'Four', }, 33 => { '1'=>'One', }, 2 => { '2' => 'Two', }, ); my $ref_HoH = \%foo; for my $k ( sort { $a <=> $b } keys %$ref_HoH ) { print "$k\n"; }
hf@flux[30] perl /tmp/testit 2 11 33
I'll leave the rest as an exercise for the OP.

~Jeff

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://559975]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-09-17 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (22 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.