Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

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

Your line for my $key1 (keys %rankBased) { implies that you have an HoHoH structure to start with and, as others have mentioned, you can't have "ordered" hashes without a bit of jiggery-pokery. To retain a sorted order you need to introduce an array in there somewhere. The following code constructs an HoAoHoH structure, inserting an array between your $key1 and $key2 so that the sort is preserved.

use strict; use warnings; use Data::Dumper; my %rankBased = ( q{191} => { test1 => { score => 9.18 }, test2 => { score => 2.84 }, test3 => { score => 15.62 }, test4 => { score => 11.84 }, }, q{190} => { test1 => { score => 13.28 }, test2 => { score => -47.56 }, test3 => { score => 18.50 }, test4 => { score => 14.88 }, }, ); my %sortedByRank; foreach my $key ( keys %rankBased ) { $sortedByRank{ $key } = [ map { { $_ => { score => $rankBased{ $key }->{ $_ }->{ score } + } } } sort { $rankBased{ $key }->{ $b }->{ score } <=> $rankBased{ $key }->{ $a }->{ score } } keys %{ $rankBased{ $key } } ]; } print Data::Dumper->Dumpxs( [ \ %sortedByRank ], [ qw{ *sortedByRank } + ] );

The Data::Dumper output.

%sortedByRank = ( '191' => [ { 'test3' => { 'score' => '15.62' } }, { 'test4' => { 'score' => '11.84' } }, { 'test1' => { 'score' => '9.18' } }, { 'test2' => { 'score' => '2.84' } } ], '190' => [ { 'test3' => { 'score' => '18.5' } }, { 'test4' => { 'score' => '14.88' } }, { 'test1' => { 'score' => '13.28' } }, { 'test2' => { 'score' => '-47.56' } } ] );

I hope this guess at your data and intentions is helpful.

Update: Changing the map line to

map { { $_ => { %{ $rankBased{ $key }->{ $_ } } } } }

will work if the innermost hashes have multiple key/value pairs.

Cheers,

JohnGG


In reply to Re: Sorting perl hash by johngg
in thread Sorting perl hash by paul05

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 surveying the Monastery: (6)
As of 2024-04-19 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found