in reply to sorting a hash by key

Your node title says you wish to sort by key. Your question says you would like to sort by value. And your script is sorting by key. 2:1, I'm going to assume you wish to sort by key. In that case, your problem is that you're doing an ascii-betical sort instead of a numeric sort. Try this:

foreach $number ( sort { $a <=> $b } keys %name ) { print "\t$number\t\t$name{$number}\n"; }

You may have another problem too. Hash keys are unique. That means you can only have one of each 'number'. You're ok if you keep that in mind. But if the numbers represent, for example, scores, it would be possible that different names are associated with the same numbers, in which case you lose some filenames when converting to a hash. This may not be an issue for you. In your example file it looks (from the three lines you showed) like there are no duplicate numbers. Just make sure that's really the case.

Also, don't forget to chomp before splitting, unless you want your filenames to come with newlines attached to them.


Dave