Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: need help sorting a multilevel hash by a particular key

by BrowserUk (Patriarch)
on Dec 13, 2013 at 15:18 UTC ( [id://1067026]=note: print w/replies, xml ) Need Help??


in reply to need help sorting a multilevel hash by a particular key

First construct a list of anonymous arrays containing just the information needed -- name|suffix|corner|value -- then sort, then build your final hash:

#! perl -slw use strict; use Data::Dump qw[ pp ]; my %hohohoh = map{ 'meas_'.$_ => { map { 'suffix_' . $_ => { map { 'C' . $_ => { value => rand 20 } } 1 .. 4 } } 1 .. 3 } } 1 .. 2; pp \%hohohoh; my %ordered; push @{ $ordered{ $_->[0] }{ $_->[ 2 ] } }, $_->[1] for sort { $a->[2] cmp $b->[2] || $a->[3] <=> $b->[3] } map { my $name = $_; map{ my $suffix = $_; map[ $name, $suffix, $_, $hohohoh{$name}{$suffix}{$_}{value} ], keys %{ $hohohoh{$name}{$suffix } }; } keys %{ $hohohoh{$name} }; } keys %hohohoh; pp \%ordered; __END__ C:\test>junk62 { meas_1 => { suffix_1 => { C1 => { value => "16.4599609375" }, C2 => { value => "18.387 +451171875" }, C3 => { value => "14.3621826171875" }, C4 => { value => "15. +8447265625" }, }, suffix_2 => { C1 => { value => "9.144287109375" }, C2 => { value => "8.103 +6376953125" }, C3 => { value => "8.3502197265625" }, C4 => { value => "5.13 +24462890625" }, }, suffix_3 => { C1 => { value => "5.531005859375" }, C2 => { value => "12.16 +85791015625" }, C3 => { value => "18.5546875" }, C4 => { value => "1.1138916 +015625" }, }, }, meas_2 => { suffix_1 => { C1 => { value => "16.4349365234375" }, C2 => { value => "7.0 +1171875" }, C3 => { value => "19.0521240234375" }, C4 => { value => "1.4 +508056640625" }, }, suffix_2 => { C1 => { value => "19.193115234375" }, C2 => { value => "19.6 +575927734375" }, C3 => { value => "1.6729736328125" }, C4 => { value => "1.95 +25146484375" }, }, suffix_3 => { C1 => { value => "2.4798583984375" }, C2 => { value => "5.77 +20947265625" }, C3 => { value => "4.3511962890625" }, C4 => { value => "4.02 +099609375" }, }, }, } { meas_1 => { C1 => ["suffix_3", "suffix_2", "suffix_1"], C2 => ["suffix_2", "suffix_3", "suffix_1"], C3 => ["suffix_2", "suffix_1", "suffix_3"], C4 => ["suffix_3", "suffix_2", "suffix_1"], }, meas_2 => { C1 => ["suffix_3", "suffix_1", "suffix_2"], C2 => ["suffix_3", "suffix_1", "suffix_2"], C3 => ["suffix_2", "suffix_3", "suffix_1"], C4 => ["suffix_1", "suffix_2", "suffix_3"], }, }

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-03-29 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found