Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

about order in hash keys

by spx2 (Deacon)
on Aug 30, 2007 at 09:56 UTC ( #636036=perlquestion: print w/replies, xml ) Need Help??
spx2 has asked for the wisdom of the Perl Monks concerning the following question:

I sometimes needed to know if I would say keys %hash in wich order would they come out ? Ofcourse I would like them to come out in the order that I put them when I created the hash. I was disappointed and then I found how hashes work and somehow felt that order in keys of a hash didn't have any reason to exist. But at page 91 in Perl Best Practices , Damian Conway talks about order amongst keys of a hash.

Question: Can one predict this order ? How ?

Replies are listed 'Best First'.
Re: about order in hash keys
by bruceb3 (Pilgrim) on Aug 30, 2007 at 10:50 UTC
    If you really want ordered hashes, there are a couple of CPAN modules. Start with LLHash.
Re: about order in hash keys
by zentara (Archbishop) on Aug 30, 2007 at 11:29 UTC
    How about
    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.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: about order in hash keys
by apl (Monsignor) on Aug 30, 2007 at 11:49 UTC
    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.
Re: about order in hash keys
by fenLisesi (Priest) on Aug 30, 2007 at 10:19 UTC
    The question is moot. Even if you could, you shouldn't rely on it. Cheers.
Re: about order in hash keys
by dsheroh (Prior) on Aug 30, 2007 at 15:09 UTC
    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.

Re: about order in hash keys
by Cristoforo (Deacon) on Aug 30, 2007 at 19:56 UTC
    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).


    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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://636036]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2017-06-27 13:12 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (606 votes). Check out past polls.