If you really want ordered hashes, there are a couple of CPAN modules. Start with LLHash. | [reply] |
foreach my $key(sort keys %hash){......}
#or numeric sort
foreach my $key( sort {$a<=>$b} keys %hash){......}
if you want them in the exact insertion order
my $index=0;
for(1..100){
$hash{$index}{'input1'} = $input1;
$hash{$index}{'input2'} = $input2;
#etc......
$index++;
}
hashes are so flexible, you just need to figure out the scheme
you need.
| [reply] [d/l] [select] |
You could associate the insertion order with the hash key.
$hash{ $key }{order} = $InsertionOrder++;
Then you could continue to use $hash{ $key } as before. When you need the information in insertion order, you could search the hash for the desired {order} value. | [reply] [d/l] [select] |
The question is moot. Even if you could, you shouldn't rely on it. Cheers. | [reply] |
The only way that you can predict the order is to build the hash, look at the order of the keys, and then expect that, if you continue using the exact same version of perl on the same hardware and building the hash in the same order, then the order of the keys will probably be the same in the future. This isn't terribly useful, though, since it doesn't allow you to control the order, even without the issue that the order is likely to change about 5 minutes after you start depending on it, if not sooner.
Personally, if I wanted to be able to retrieve from a hash in insertion order, I'd probably build a parallel array and push each new key onto it as it's inserted into the hash. Then I could use the hash itself for random access and look up the appropriate hash keys in the array for insertion-ordered access. | [reply] |
This subject was discussed here hash randomization and keys(). On my ActiveState build, I think I get the same order of keys between runs and this may be a perl compiled as described below (from the perlrun document).
Chris
PERL_HASH_SEED
(Since Perl 5.8.1.) Used to randomise Perl's internal hash function. To emulate the pre-5.8.1 behaviour, set to an integer (zero means exactly the same order as 5.8.0). ``Pre-5.8.1'' means, among other things, that hash keys will be ordered the same between different runs of Perl.
The default behaviour is to randomise unless the PERL_HASH_SEED is set. If Perl has been compiled with -DUSE_HASH_SEED_EXPLICIT, the default behaviour is not to randomise unless the PERL_HASH_SEED is set.
If PERL_HASH_SEED is unset or set to a non-numeric string, Perl uses the pseudorandom seed supplied by the operating system and libraries. This means that each different run of Perl will have a different ordering of the results of keys(), values(), and each().
Please note that the hash seed is sensitive information. Hashes are randomized to protect against local and remote attacks against Perl code. By manually setting a seed this protection may be partially or completely lost.
See Algorithmic Complexity Attacks in the perlsec manpage and PERL_HASH_SEED_DEBUG for more information.
| [reply] |