G'day Special_K,

Your prosaic description of the data structure is not entirely clear. I've made the assumption that "Each corner will have the same number of suffixes for a given meas name." means you have a structure like this:

{ meas_1 => { suffix_1 => { C1 => { value => 10.2 }, C2 => { value => 110.2 } }, suffix_2 => { C1 => { value => 4.7 }, C2 => { value => 204.7 } }, suffix_3 => { C1 => { value => 5.2 }, C2 => { value => 305.2 } }, suffix_4 => { C1 => { value => 11.8 }, C2 => { value => 411.8 } }, suffix_5 => { C1 => { value => 0.7 }, C2 => { value => 500.7 } }, }, }

If that assumption is incorrect, please provide a clearer description of what you do have (showing example data instead of a narrative).

Based on my assumption, this code does what you want:

#!/usr/bin/env perl use strict; use warnings; my (%data, %ordered_hash); $data{meas_1}{suffix_1}{C1}{value} = 10.2; $data{meas_1}{suffix_2}{C1}{value} = 4.7; $data{meas_1}{suffix_3}{C1}{value} = 5.2; $data{meas_1}{suffix_4}{C1}{value} = 11.8; $data{meas_1}{suffix_5}{C1}{value} = 0.7; $data{meas_1}{suffix_1}{C2}{value} = 110.2; $data{meas_1}{suffix_2}{C2}{value} = 204.7; $data{meas_1}{suffix_3}{C2}{value} = 305.2; $data{meas_1}{suffix_4}{C2}{value} = 411.8; $data{meas_1}{suffix_5}{C2}{value} = 500.7; for my $meas (keys %data) { my @suffixes = keys %{$data{$meas}}; for my $corner (keys %{$data{$meas}{$suffixes[0]}}) { push @{$ordered_hash{$meas}{$corner}}, map { $_->[0] } sort { $b->[1] <=> $a->[1] } map { [ $_ => $data{$meas}{$_}{$corner}{value} ] } @suffix +es; } } use Data::Dump; dd \%ordered_hash;

Output:

{ meas_1 => { C1 => ["suffix_4", "suffix_1", "suffix_3", "suffix_2", "suffix_5"] +, C2 => ["suffix_5", "suffix_4", "suffix_3", "suffix_2", "suffix_1"] +, }, }

-- Ken


In reply to Re: need help sorting a multilevel hash by a particular key by kcott
in thread need help sorting a multilevel hash by a particular key 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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.