$VAR1 = { 'host' => { 'batch' => { 'run1' => { 'jobs' => { 'Job name 1' => { 'duration' => '16', 'end' => '2011/07/01 +23:50:36', 'start' => '2011/07/0 +1 23:50:19' }, 'Job name 2' => { 'duration' => '22', 'end' => '2011/07/02 + 05:22:29', 'start' => '2011/07/ +02 05:22:06' }, }, 'duration' => '25' 'end' => '2011/07/02 05:22:29', 'start' => '2011/07/02 05:22:06' }, 'run2' => { 'jobs' => { 'Job name 1' => { 'duration' => '16', 'end' => '2011/07/01 +23:50:36', 'start' => '2011/07/0 +1 23:50:19' }, 'Job name 2' => { 'duration' => '22', 'end' => '2011/07/02 + 05:22:29', 'start' => '2011/07/ +02 05:22:06' }, }, 'duration' => '25' 'end' => '2011/07/02 05:22:29', 'start' => '2011/07/02 05:22:06' }, }, }, };
Hash is layed out like this:
%Hash -> $host -> $batch -> $run -> 'jobs' -> $jobname -> 'durati +on' -> 'duration' -> 'end' -> 'start' -> 'start' -> 'end'

Problem:
I would like to sort the hash by values such as:
- $Hash->{$cluster}->{$host}->{$batch}->{'start'}
- $Hash->{$cluster}->{$host}->{$batch}->{'jobs'}->{$jobname}->{'start'}
Therefore sorting by the 'start' values of the batch and jobs

foreach $host ( keys %hash ) { foreach $batch ( keys %{$hash{$host}} ) { foreach $run ( keys %{$hash{$host}{$batch}} ) { #print $hash{$host}{$batch}{$run}{'duration'}, "\n"; print $hash{$host}{$batch}{$run}{'start'}, "\n"; #How do +I sort by this? #print $hash{$host}{$batch}{$run}{'end'}, "\n"; foreach $job ( keys %{$hash{$host}{$batch}{$run}{'jobs'} ) { print $hash{$host}{$batch}{$run}{'jobs'}{$job}{'start' +}, "\n"; #How do I sort by this? } } } }

I have tried using the sort { hash{cluster}{host}{batch}{$a}->{duration} <=> hash{cluster}{host}{batch}{$b}->{duration} } but it did not produce expected results. I also tried other variations I found online and searching through here.

Hoping someone could point me in the right direction, please!


In reply to Sorting hash of hash of hash by values by legendx

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.