Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Sorting a hash in a predetermined order

by johngg (Canon)
on Nov 17, 2015 at 23:13 UTC ( [id://1147962]=note: print w/replies, xml ) Need Help??


in reply to Sorting a hash in a predetermined order

Store the order you want in an array.

$ perl -Mstrict -Mwarnings -E ' my %people; $people{ Fred }->{ age } = 37; $people{ Fred }->{ sex } = q{male}; $people{ Fred }->{ occ } = q{plumber}; $people{ Mary }->{ age } = 41; $people{ Mary }->{ sex } = q{female}; $people{ Mary }->{ occ } = q{barmaid}; my @order = qw{ age sex occ }; foreach my $person ( keys %people ) { say qq{$person:}; foreach my $attr ( @order ) { say qq{ $attr: $people{ $person }->{ $attr }}; } }' Mary: age: 41 sex: female occ: barmaid Fred: age: 37 sex: male occ: plumber $

I hope this is helpful.

Cheers,

JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1147962]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-19 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found