Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

sorting hash in a non alpabellical order

by madM (Beadle)
on Jan 13, 2014 at 11:32 UTC ( [id://1070395]=perlquestion: print w/replies, xml ) Need Help??

madM has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I wanted to ask you all if it is possible to sort a hash in a specific order... for example if i have a hash with this keys
A B C D E F G H I K L M N P Q R S T V W X Y Z
and i want to order them in this specific order like this
A R N D C Q E G H I L K M F P S T W Y V B Z X
thanks fot the help!

Replies are listed 'Best First'.
Re: sorting hash in a non alpabellical order
by Corion (Patriarch) on Jan 13, 2014 at 11:35 UTC

    A hash has no order.

    But maybe you want to output the elements of a hash in a particular order. Then you can use the following approach with a "hash slice" for example:

    my @columns= qw(A R N D C Q E G H I L K M F P S T W Y V B Z X); my %hash= ...; print "@columns\n"; print "@hash{ @columns }\n";

    The alternative approach is to simply use a loop over @columns.

      thank you! this helps me a lot :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-26 02:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found