Perl: Sort A Hash?

Creedyou

Senior member
Dec 28, 2001
205
0
0
I need to sort a hash by its key. I can't get this to work because I can't figure out how to keep the hash's values and keys together. What I was trying to do was extract the keys to an array using keys %hash, but then I don't know how to match the keys with its value.
 

Creedyou

Senior member
Dec 28, 2001
205
0
0
i am not sure what you are trying to do. are u saying make an array of each hash key and value as 1 element in an array?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Creedyou
i am not sure what you are trying to do. are u saying make an array of each hash key and value as 1 element in an array?


# This is what you tried before, right?
@array = keys %hash;
@array = sort @array;

# But you didn't know how to get the values back from the keys, so I suggested:
for(my $i =0; $i < @array;$i++){
print $hash{$array[$i]}
}