Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Answer: How do I print the values of a hash, sorted by the hash keys?

by Russ (Deacon)
on Jan 12, 2007 at 19:39 UTC ( #594435=categorized answer: print w/replies, xml ) Need Help??

Q&A > sorting > How do I print the values of a hash, sorted by the hash keys? - Answer contributed by Russ

There's more than one way to do it.

Here's the obvious way:

foreach my $key ( sort keys %data ) { print $data{$key}; }

Here it is, using a map within a single print call:

print map { $data{$_} } sort keys %data;

Here's a way using a hash slice:

foreach my $val ( @data{ sort keys %data } ) { print $val; }

Even the slice can be printed in a single print call:

print @data{ sort keys %data };

Of course, you'll want to set up the variables $, and $\ appropriately...

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2017-11-19 05:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (278 votes). Check out past polls.

    Notices?