Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: I don't understand why keys %hash does this ...

by Roger (Parson)
on Oct 05, 2004 at 06:20 UTC ( [id://396493]=note: print w/replies, xml ) Need Help??


in reply to I don't understand why keys %hash does this ...

The monks have pointed out that hash order is not preserved. I just want to point out that one method I often use to preserve the order of the input hash is to use an intermediate array:

#!/usr/bin/perl use strict; use Data::Dumper; my %HoTypes = my @HoTypes = ( 'PythonQuestions' => [ '[PythonQuestions|Python Questions]', 318, +3, 10 ], 'OffTopics' => [ '[OffTopics|Off Topic Post]', 500, +3, 10 ], 'notes' => [ 'Notes', 283, +3, 20 ] ); # retrieve the keys in the original order @HoTypes = @HoTypes[map { $_ % 2 ? () : $_ } 0..$#HoTypes]; # and use the keys recorded in the array for my $T ( @HoTypes ) { my $nodeStr = $HoTypes{$T}[0]; print "$T -> $nodeStr\n"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-26 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found