You need to tie each of the hashes that you want order preserved in. When you do:
tie (my %data, 'Tie::IxHash');
That ties the hash %data as a Tie::IxHash, but never does anything about the values in %data. (keep in mind that ALL hash values are scalars).

You'll need something like this:

tie(my %data, 'Tie::IxHash'); while ( ... ) { # add info to %data if ( <working in a second tier> ) { if ( ! exists $data{$top_level_key) ) { $data{$top_level_key} = {}; tie ( %{$data{$top_level_key}}, 'Tie::IxHash' ); } ... } ... }
Without knowing how you're creating these nested hashes, i can't give better insight, but hopefully my little framework will express the idea to you.

UPDATE
The following suggestion already exists, as Borisz points out below.
End update

If you wanted to (if it suits your needs), you could cook up a more complicated version of Tie::IxHash (call it something else - like Tie::IxHash::MultiLevel), that checks if values being added to the hash are HASHREF's, and if they are, then tie them also.

I don't particularly recommend this approach, because it could bite you later on. It will be automatically recursive down the levels though, because if a second tier hashref is tied as a Tie::IxHash::MultiLevel, then it will do checking on its values too.


I use the most powerful debugger available: print!

In reply to Re: Preserving order for nested hashes using Tie::IxHash by shemp
in thread Preserving order for nested hashes using Tie::IxHash by ramya2005

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.