Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re: Sorting a Hash of Hashes by HuckinFappy
in thread Sorting a Hash of Hashes by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-03-28 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found