Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: 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 ( [id://594435]=note: print w/replies, xml ) Need Help??


in reply to How do I print the values of a hash, sorted by the hash keys?

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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://594435]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-20 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found