in reply to Sorting hash of hash of hash by values

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

Replies are listed 'Best First'.
Re^2: Sorting hash of hash of hash by values
by legendx (Acolyte) on Jul 07, 2011 at 21:45 UTC
    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?

        I guess I was not 100% clear on how it worked