in reply to Problem with printing a Hash of Hashes in order

Unless you use key based lookup for the data elsewhere you would probably be better to use a nested array structure instead of the nested hash structure you are currently using. Consider:

#!c:\Perl\bin\perl use warnings; use strict; use constant KANRI_01_0 => 0x00000001; # TBC use constant KANRI_01_1 => 0x00000002; # TBC use constant KANRI_02_0 => 0x00000004; # TBC use constant KANRI_02_1 => 0x00000008; # TBC use constant KANRI_03_0 => 0x00000010; # TBC use constant KANRI_04_1 => 0x00000020; # TBC use constant KANRI_04_2 => 0x00000040; # TBC use constant KANRI_04_3 => 0x00000080; # TBC use constant KANRI_04_4 => 0x00000100; # TBC use constant KANRI_05_1 => 0x00000200; # TBC use constant KANRI_05_2 => 0x00000400; # TBC use constant KANRI_06_1 => 0x00000800; # TBC use constant KANRI_07_1 => 0x00001000; # TBC use constant KANRI_07_2 => 0x00002000; # TBC use constant KANRI_07_3 => 0x00004000; # TBC use constant KANRI_07_4 => 0x00008000; # TBC use constant KANRI_07_5 => 0x00010000; # TBC use constant KANRI_08_1 => 0x00020000; # TBC my @menu = ( [Outer1 => [ [O1Inne1 => KANRI_04_1], [O1Inne2 => KANRI_04_2], [O1Inne3 => KANRI_04_3], [O1Inne4 => KANRI_04_4], ]], [Outer2 => [[O2Inne1 => KANRI_05_1]]], [Outer3 => [[O3Inne1 => KANRI_06_1]]], [Outer4 => [ [O4Inne1 => KANRI_07_1], [O4Inne2 => KANRI_07_2], [O4Inne3 => KANRI_07_3], [O4Inne4 => KANRI_07_4], [O4Inne5 => KANRI_07_5], ]], [Outer5 => [[O5Inne1 => KANRI_08_1]]], ); for my $mainMenuItem (@menu) { print "$mainMenuItem->[0]\n"; for my $subMenuItem (@{$mainMenuItem->[1]}) { print "$subMenuItem->[0] = $subMenuItem->[1] "; } print "\n"; }

Prints:

Outer1 O1Inne1 = 32 O1Inne2 = 64 O1Inne3 = 128 O1Inne4 = 256 Outer2 O2Inne1 = 512 Outer3 O3Inne1 = 2048 Outer4 O4Inne1 = 4096 O4Inne2 = 8192 O4Inne3 = 16384 O4Inne4 = 32768 +O4Inne5 = 65536 Outer5 O5Inne1 = 131072
True laziness is hard work

Replies are listed 'Best First'.
Re^2: Problem with printing a Hash of Hashes in order
by AnomalousMonk (Archbishop) on Jan 27, 2011 at 05:29 UTC

    I was thinking of basically the same thing, but with fewer square brackets thanks to some help from List::MoreUtils::natatime():

    >perl -wMstrict -le "use List::MoreUtils qw(natatime); ;; my @menuStruct = ( o1 => [ o1_i1 => 0x11, o1_i2 => 0x12, o1_i3 => 0x13, o1_i4 => 0x14, ], o2 => [ o2_i1 => 0x21 ], o3 => [ o3_i1 => 0x31 ], o4 => [ o4_i1 => 0x41, o4_i2 => 0x42, o4_i3 => 0x43, ], o5 => [ o5_i1 => 0x51 ], ); ;; my $o_iter = natatime 2, @menuStruct; while (my ($ok, $ov) = $o_iter->()) { print qq{$ok}; my $i_iter = natatime 2, @$ov; while (my ($ik, $iv) = $i_iter->()) { printf qq{ $ik = %x \n}, $iv; } } " o1 o1_i1 = 11 o1_i2 = 12 o1_i3 = 13 o1_i4 = 14 o2 o2_i1 = 21 o3 o3_i1 = 31 o4 o4_i1 = 41 o4_i2 = 42 o4_i3 = 43 o5 o5_i1 = 51

    I think either of these approaches could be made recursive.