I have a simple hash with directory names as keys and sizes as values. I would like to print the hash, in descending order of the size of each directory.
I was contemplating of "reversing" the hash(so that sizes become keys and directory names become values) and then do a sort on the keys. However, this approach would not work if two directories have the same sizes, as that would "flatten" the keys list! In my code snippet below, C:/rex and C:/mail have same sizes and therefore the end-result would have just one of the two. Therefore, is there a straight-forward way that I am missing? Of course, I can do some extra array-processing to report the results. I am just curious to find if there is a straight-forward way. Thanks, Rex ############################### #!/usr/bin/perl use strict; use warnings; my(%hash); $hash{'c:/rex'} = 100; $hash{'c:/perl'} = 50; $hash{'c:/temp'} = 75; $hash{'c:/mail'} = 100; print("\n\nBefore Reversing.... \n"); map{ print("$_ ===> $hash{$_}\n") } keys %hash; %hash = reverse(%hash); print("\n\nAfter Reversing and Sorting...\n"); map{ print("$hash{$_} ===> $_ \n") } sort{$b <=> $a} keys %hash; ###############################