legendx has asked for the wisdom of the Perl Monks concerning the following question:
Hash is layed out like this:$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 -> $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 | |
by legendx (Acolyte) on Jul 07, 2011 at 21:45 UTC | |
by jethro (Monsignor) on Jul 08, 2011 at 08:56 UTC | |
by legendx (Acolyte) on Jul 08, 2011 at 14:47 UTC |