in reply to How to sort Hashes of Hashes and print results in a file

Is this what you want?
#!/usr/bin/perl -w use strict; my %hash = ( 'c' => { 'e' => '54.9503577512828', '21' => '40.4544882398113', '7' => '13.665886711539', '17' => '26.3268082750993', '2' => '16.4348644122811', '22' => '35.2357587506792', '1' => '29.5471128362965', '18' => '28.8861232027377', '23' => '19.3437720284897', }, 'd' => { '11' => '26.7647206436788', '21' => '19.4128184125387', '7' => '6.89345884629317', '17' => '12.7682648883531', '2' => '8.44015343941174', '22' => '16.9085166076037', '1' => '15.1739716114483', '18' => '13.9203445318945', }, 'b' => { '11' => '11.5303855045498', '21' => '9.28718107649433', '7' => '2.50040155572952', '17' => '5.93010343000324', '2' => '2.86098737229312', } ); foreach my $k1 (sort keys %hash){ print "\n-> $k1\n"; my $count = 0; foreach my $k2 (sort {$hash{$k1}->{$b} <=> $hash{$k1}->{$a} } keys +%{$hash{$k1}} ){ print "--> $k2: $hash{$k1}->{$k2}\n"; last if $count++ > 0; } }

Replies are listed 'Best First'.
Re^2: How to sort Hashes of Hashes and print results in a file
by stan131 (Acolyte) on Mar 28, 2009 at 00:33 UTC
    Thanks a million gulden. Your script results are exactly what I want.
    I was introduced to perl only a week ago so still learning as I code more.

    Once again Thanks all for your help. Very much appreciated
    Stan
      If you want to sort "numbers" instead of "strings" for the first level keys of the hash, then replace
      foreach my $k1 (sort keys %hash){
      with
      foreach my $k1 (sort {$a <=> $b} keys %hash){