Gossamer Forum
Home : General : Perl Programming :

Sorting hashes

Quote Reply
Sorting hashes
I am relatively new to Perl. Here is the line of code that I have written.It returns key value pairs that I use for further processing. However, I want it to return the pairs in a sorted list (sorted by the keys)

while(($key, $value) = each %hash)
{
...
}

Is there any way in which I can sort the hash within this line or separately before executing this line of code.

Your help is greatly appreciated.
Quote Reply
Re: [deep] Sorting hashes In reply to
Code:
for (sort { lc $a cmp $lc $b } keys %hash) {

The key will be stored in $_ and the value in $hash{$_}

http://www.perldoc.com/...6/pod/func/sort.html

Last edited by:

Paul: Oct 7, 2002, 8:50 AM
Quote Reply
Re: [Paul] Sorting hashes In reply to
Cool Thanks very much, worked like a charm. Also, thanks for the link. Looks very helpful.
Quote Reply
Re: [deep] Sorting hashes In reply to
Oops make sure you remove the $ infront of that second lc - typo Blush

Last edited by:

Paul: Oct 7, 2002, 9:12 AM