Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^5: Maintenance of element order in hash

by hippo (Bishop)
on Sep 30, 2016 at 08:33 UTC ( [id://1172980]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Maintainance of element order in hash
in thread Maintainance of element order in hash

There is no point whatsoever in tieing a hash like %foo to retain the order of its keys when it only has one key as in your example, which is "a". Likewise any subhashes which are untied will not retain the order of their keys. If you wish to retain the order all the way down for some reason then you need to tie all your hashes.

#!/usr/bin/env perl # Subhash order maintained use strict; use warnings; use Test::More tests => 3; use Tie::IxHash; tie my %a, 'Tie::IxHash'; tie my %b, 'Tie::IxHash'; tie my %x, 'Tie::IxHash'; my %foo; $a{c} = [3, 2, 1]; %x = ( s => 'senatus', p => 'populus', q => 'que', r => 'romanus'); %b = ( x => \%x, y => "ravi" ); $a{b} = \%b; $foo{a} = \%a; is_deeply ([keys %{$foo{a}}], [qw/c b/], '1st level order re +tained'); is_deeply ([keys %{$foo{a}{b}}], [qw/x y/], '2nd level order re +tained'); is_deeply ([keys %{$foo{a}{b}{x}}], [qw/s p q r/], '3rd level order re +tained');

This all still very much sounds like an XY Problem, however.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-19 20:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found