Perl: Sorting a hash

This syntax still doesn't make much sense to me but here is how you sort a perl hash by value. This returns a list of all the keys of the hash sorted in the order you want. To reverse the sort simply change $a and $b locations with each other.

my @sort = sort{ $unique{$a} <=> $unique{$b} } keys %unique;
Tags:
Leave A Reply - 2 Replies
Replies
January 15th 2007 - Ravindra

Thanks Man!! Its solved my problem..

December 10th 2008 - Anonymous

reversing a and b didnt work for me, however doing a @sort = reverse sort{ ... worked! Thanks

All content licensed under the Creative Commons License