in reply to Hashes do preserve insertion order after all

kikuchiyo:

No, it's letting you sort by the address, as you mention. So it's giving you the creation order instead of the insertion order.

#!/usr/bin/perl use strict; use warnings; use feature qw/say/; my %hoh = ( foo => { value => 'first' }, bar => { value => 'second' }, baz => { value => 'third' }, ); my %h2; $h2{first} = $hoh{baz}; $h2{second} = $hoh{foo}; $h2{third} = $hoh{bar}; for my $elem (sort values %h2) { say "value => " . $elem->{value}; }

Here, I'm inserting the same items into another hash in a different order, yet it returns the same order as the first.

$ perl p.pl value => first value => second value => third

The creation order isn't guaranteed, either. Once you start adding and deleting objects over a longer time period, deleted chunks of memory can be reused later breaking the correlation of address and creation time.

...roboticus

When your only tool is a hammer, all problems look like your thumb.