Hello Monks,

I have to read a text file and build a multiple level data structure and display in the same order it was read in.

I found that Tie::IxHash module can make that happen but I can't make it work the way I wanted it to.

First, If I simple create a data strcuture as shown below and try to print it, it doesn't print in the order I created the hash.

use Data::Dumper; use Tie::IxHash; tie my %hash_ref, 'Tie::IxHash' ; %hash_ref = ( 'MAIN' => { 'ZOP' => {'Dev', undef, 'Con', undef, 'Test', undef, 'Exit', undef, 'New', undef }, 'AP' => {'Dev', undef, 'Con', undef, 'Test', undef, 'Exit', undef, 'New', undef }, 'Exit' => undef, + } ); print Dumper(\%hash_ref);

Second, I can create individual hash variables and then build the structure but here also I have the same problem - the order is getting changed while printing

use Data::Dumper; use Tie::IxHash; tie my %list1, 'Tie::IxHash' ; tie my %list2, 'Tie::IxHash' ; tie my %hnr, 'Tie::IxHash' ; %list1 = ( 'ZOP',undef,'AP',undef, 'Exit', undef ); %list2 = ( 'Dev',undef, 'Con', undef, 'Test', undef, 'Exit', undef, 'N +ew', undef); $hnr{'MAIN'} = { %list1 }; $hnr{'MAIN'}{'ZOP'} = { %list2 }; $hnr{'MAIN'}{'AP'} = { %list2 }; print Dumper(\%hnr);

But the below example (simple hash) works perfectly...

tie my %myhash, 'Tie::IxHash'; %myhash = ( 'ne', undef, 'na', undef, 'e', undef, 'a', undef, 'z', undef, 'h', undef, 2, undef, 5, undef, 12, undef, 7, undef); print Dumper(\%myhash);
Hence, I am not sure as to what is going wrong in the multi-level structure as compared to the simple hash. Would someone please help me with this?

In reply to Trouble with Tie::IxHash by sara2005

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.