legendx has asked for the wisdom of the Perl Monks concerning the following question:

$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!

Replies are listed 'Best First'.
Re: Sorting hash of hash of hash by values
by jethro (Monsignor) on Jul 07, 2011 at 17:04 UTC

    You don't seem to be far off, but I hope you didn't fall into the trap of trying to sort the hash itself. Hashes can't be sorted, but arrays or lists made from their keys.

    foreach $batch ( keys %{$hash{$host}} ) { my @runs= keys %{$hash{$host}{$batch}; @runs= sort { $hash{$host}{$batch}{$a}{start} <=> $hash{$host}{$bat +ch}{$b}{start} } @runs; foreach $run (@runs) { ...

    Naturally you can compress this into one line and remove the need for @runs, but I wanted to show explicitly what I was doing

    Oh and it is untested. Wouldn't suprise me if I had made a silly mistake somewhere

      Hey thanks! that worked. I was able to sort by the $batch 'duration' values
      I was using  sort { $hash{$host}{$batch}{$run}{$a}{start} <=> $hash{$host}{$batch}{$run}{$b}{start} } instead and it wasn't working.

      Why does hash{$host}{$batch}{$a}{start} <=> $hash{$host}{$batch}{$b}{start} work when I am not specifying the $run? Is it because Perl puts the two elements it wants to compare (which is the $run) into the special variables $a and $b?

        Yes, $a and $b are aliases to values that get compared. Even if it were not it wouldn't make sense to add more dereferencing to a hash structure than there is depth.

        Just out of curiosity: Why did you write $a and $b into your code if you didn't know that?