perl - How are hash keys sorted? -


until today, thought hash keys randomised when returned. however, entry in perldoc on keys suggests otherwise:

hash entries returned in apparently random order. actual random order specific given hash; exact same series of operations on 2 hashes may result in different order each hash.

there other entries on relate - pertinent accepted answer this question.

the following code returns apparently randomised hash keys:

 %hash;  $hash{$_}++ 1 .. 100;    keys %hash; 

can me understand how not actually random?

how hash keys sorted depends on version of perl using. should not depend on seeming order associated version. includes assuming somehow randomization involved appropriate used in other circumstances statistical qualities of randomness matters.

from 5.18 hash overhaul:

hash overhaul

changes implementation of hashes in perl v5.18.0 1 of visible changes behavior of existing code.

by default, 2 distinct hash variables identical keys , values may provide contents in different order identical.

when encountering these changes, key cleaning them accept hashes unordered collections , act accordingly.

hash randomization

the seed used perl's hash function random. means order keys/values returned functions keys(), values(), , each() differ run run.

this change introduced make perl's hashes more robust algorithmic complexity attacks, , because discovered exposes hash ordering dependency bugs , makes them easier track down.

toolchain maintainers might want invest in additional infrastructure test things this. running tests several times in row , comparing results make easier spot hash order dependencies in code. authors encouraged not expose key order of perl's hashes insecure audiences.

further, every hash has own iteration order, should make more difficult determine current hash seed is.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -