Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I first approached this by forming a "flattened" intermediate hash where the first and second level keys were joined with a NUL character. The keys of the new hash could then be sorted by the associated descending numeric values then used for the printf statement. This code

use strict; use warnings; my %hoh_test = ( foo1 => { bar => -0.12697, baz => -0.000398154, }, foo2 => { bar => -4.0183e-05, baz => 0, }, foo3 => { bar => 9.966003977e-06, baz => 0.0001939, }, ); my %flatter = map { my $k1 = $_; map { join( qq{\0}, $k1, $_ ), $hoh_test{ $k1 }->{ $_ } } keys %{ $hoh_test{ $k1 } }; } keys %hoh_test; my @sortedKeys = sort { $flatter{ $b } <=> $flatter{ $a } } keys %flatter; printf qq{foo: %s, ba: %s, value: %s\n}, split( m{\0}, $_ ), $flatter{ $_ } for @sortedKeys;

produces

foo: foo3, ba: baz, value: 0.0001939 foo: foo3, ba: bar, value: 9.966003977e-06 foo: foo2, ba: baz, value: 0 foo: foo2, ba: bar, value: -4.0183e-05 foo: foo1, ba: baz, value: -0.000398154 foo: foo1, ba: bar, value: -0.12697

This seemed a bit long-winded so I thought it might be possible to still use combined keys but mapping out array refs to perform an ST all in one fell swoop. This code

use strict; use warnings; my %hoh_test = ( foo1 => { bar => -0.12697, baz => -0.000398154, }, foo2 => { bar => -4.0183e-05, baz => 0, }, foo3 => { bar => 9.966003977e-06, baz => 0.0001939, }, ); printf qq{foo: %s, ba: %s, value: %s\n}, @{ $_ } for map { [ split( m{\0}, $_->[ 0 ] ), $_->[ 1 ] ] } sort { $b->[ 1 ] <=> $a->[ 1 ] } map { my $k1 = $_; map { [ join( qq{\0}, $k1, $_ ), $hoh_test{ $k1 }->{ $_ } ] } keys %{ $hoh_test{ $k1 } }; } keys %hoh_test;

produces identical output. I hope this is of interest.

Update: Corrected typo, s/tha/the/

Cheers,

JohnGG


In reply to Re: help sorting hash of hashes by value by johngg
in thread help sorting hash of hashes by value by Special_K

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 browsing the Monastery: (3)
As of 2024-04-23 22:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found