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.

@sort = sort{ $unique{$a} <=> $unique{$b} } keys %unique;
Leave A Reply - 2 Replies
Ravindra 2007-01-15 12:17am - No Email - Logged IP:

Thanks Man!! Its solved my problem..

Anonymous 2008-12-10 02:47pm - No Email - Logged IP:

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

All content licensed under the Creative Commons License