in reply to order of hash

Does Perl know how a particular hash will be ordered ahead of time, given it's characteristics?

I think the answer is yes. I even suspect that Perl uses the same hashing function/algorithm regardless the type/nature of the search key.

I have constructed the following code to test my hypothesis -
my %hashA; $hashA{123456} = 1; $hashA{'Roger and Albert'} = 1; $hashA{'Roger'} = 1; $hashA{'456789'} = 1; print "HASH A\n------\n"; print "$_\n" for keys %hashA; my %hashB; $hashB{'Roger'} = 1; $hashB{123456} = 1; $hashB{'Roger and Albert'} = 1; $hashB{456789} = 1; print "\nHASH B\n------\n"; print "$_\n" for keys %hashB;
And the order of both hashA and hashB are identical -
HASH A ------ 456789 123456 Roger and Albert Roger HASH B ------ 456789 123456 Roger and Albert Roger
In the first case, a number is used as a search key to start the hash, while in the second case, a string is used as a search key to start the hash. If the perl hash engine differentiate numeric search keys and string search keys, then I would get two different hash orders for the hashes. But that's not the case.

The conclusion is that Perl treats all the search keys as strings, and it has a single hashing function optimized for string search keys.

However Perl might implement different hashing functions between different versions. Perl 5.8.1 might have introduced a different random seed/number every session to initialize its hashing engine/function, perhaps to reduce the bias of the hashing function and improve the overall performance of the hash.

Replies are listed 'Best First'.
Re: Re: order of hash
by QM (Parson) on Nov 05, 2003 at 16:20 UTC
    The random seed for the hash function is to avoid DoS attacks based on "well-crafted" data. Reducing the bias of the hash function (if there was any) or improving the performance would require a different hash algorithm.

    Carelessly chosen alogorithms might be less secure against DoS attacks, be more biased, or degrade performance.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of