Wednesday, April 14, 2010

study note

Perl sort a hash
1 sort by key
foreach $key (sort (keys(%hash_name))) {
  print "\t\t$key \t\t$hash_name{$key}\n";
}
2 sort by $value 
make use of perl sort
 foreach my $key ( sort { $rules{$a} <=> $rules{$b} } ( keys %hash_name ))
{
  print "$key => $hash_name{$key}\n";
}
if you want get the set sorted numeric and desc
switch $a and $b e.g

 foreach my $key ( sort { $rules{$b} <=> $rules{$a} } ( keys %hash_name ))
{
print "$key => $hash_name{$key}\n";
}

No comments: