note
johngg
<p>Your line <c>for my $key1 (keys %rankBased) {</c> 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 <c>$key1</c> and <c>$key2</c> so that the [doc://sort] is preserved.</p>
<code>
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 } ] );
</code>
<p>The [mod://Data::Dumper] output.</p>
<code>
%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'
}
}
]
);
</code>
<p>I hope this guess at your data and intentions is helpful.</p>
<p><b>Update: </b>Changing the <c>map</c> line to</p>
<code>
map { { $_ => { %{ $rankBased{ $key }->{ $_ } } } } }
</code>
<p>will work if the innermost hashes have multiple key/value pairs.</p>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-401112">
<p>Cheers,</p><p>JohnGG</p>
</div></div>
11129199
11129199