Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Hashes do preserve insertion order after all

by roboticus (Chancellor)
on Jul 31, 2019 at 16:16 UTC ( [id://11103652]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11103652]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-16 04:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found