• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Perl: Sort A Hash?

Creedyou

Senior member
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.
 
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?
 
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]}
}
 
Back
Top