Saturday, January 29, 2011

Sorting Hash of Arrays

#!/usr/bin/perl 

%ntp_test_hash = (   
  zyxhost1 => ['1', '0.009'],   
  host2 => ['2', '-1.008'],   
  jfkhost3 => ['3', '0.003'],   
  abchost4 => ['2', '-0.003'],
); 

$size = keys %ntp_test_hash;
print "total number of hosts: $size\n"; 

foreach $host (sort { $ntp_test_hash{$b}[1] <=> $ntp_test_hash{$a}[1] } keys %ntp_test_hash ) {  
  print "$host:$ntp_test_hash{$host}[1]\n";
}

No comments: