Dear PerlMonks,

This is not as simple as the title suggest. The problem I have is that I have the below code:

#!c:\Perl\bin\perl use warnings; use strict; use diagnostics; 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 %menuHash = ( 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 ( keys %menuHash ) { print $mainMenuItem."\n"; for my $subMenuItem ( keys %{ $menuHash{$mainMenuItem} } ) { print "$subMenuItem = $menuHash{$mainMenuItem}{$subMenuIte +m} "; } print "\n"; }
This is all good and wonderful and the world turns as it should (well my code compiles and runs);

Now the problem I have is in the output. See I was expecting something like:

Outer1 01Inne1 = 32 01Inne2 = 64 01Inne3 = 128 01Inne4 = 256 Outer2 02Inne1 = 512 etc....
And if I got this it would be great... but I didn't. I got:
Outer4 O4Inne5 = 65536 O4Inne3 = 16384 O4Inne2 = 8192 O4Inne4 = 32768 + O4Inne1 = 4096 Outer2 O2Inne1 = 512 Outer1 O1Inne2 = 64 O1Inne4 = 256 O1Inne3 = 128 O1Inne1 = 32 Outer5 O5Inne1 = 131072 Outer3 O3Inne1 = 2048
Now I am running Strawberry Perl 5.12.1 on Windows XP, using the Eclipse EPIC IDE. I have ran this code from the CMD line, in Eclipse, and also through my web server as well. It all comes out like this.

What I am wanting to do is for it to print out the contents of the Hash of Hashes in the order they have been set up in. In this example the hash keys can be sorted, but in my actual code they can't (as they are alphanumerical and I don't want them sorted in that order), same with the values as well.

The reason for this structure is so that I can use this hash to print out two different levels of menu's (using CGI/HTML) and easily track the menu structure through this hash.

Would someone please be so kind as to point me in the direction of where I can find out what is causing this strange behavior (well it is strange from my POV).

I have searched on google for "problem with printing hashes", "printing order of hashes" and "sorting hashes" along with searching Perlmonks for "printing hashes", but have been unable to find anything on this particular problem. Only on using different sort methods.

Thank you in advance for taking the time at looking at this problem and for any suggestions you can offer. I will post the solution on this thread when I have one as well for others with a similar problem.

EDIT --- Link to the end solution I used

I have posted some dummy code similar to my end solution for printing a two layer HTML menu using constants as keys for hashes in this thread. This link will take you directly to the node.


In reply to Problem with printing a Hash of Hashes in order by KyussRyn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.