30.12.2014 09:04

Perl hash order randomization

After update to perl 5.18 suddenly my apps started to produce web page elements in random order. First I thought this is due to some kind of bug in my code. Then I found, new version of perl "finally" implemented hash order randomization.

I do not have a code that really relies on hash order, but I get used to some order of the statistical pages I generated with my statistic like meteolinger weatherstats. What was bad was that the element of the pages generated from hash of key-value pairs were displayed in random order now, and also colours in graphs started to change with every picture generation. Up to now, the hash order the keys were read with foreach loop was the same as the one when it was filled. I am not sure what is the correct way to use this (maybe use array) but I fixed it for me with just a sort

foreach $atribute (keys %hasharray)

to e.g.

foreach $atribute (reverse sort keys %hasharray)

This stabilizes the order of the values at least.

Email comment