sara2005 has asked for the wisdom of the Perl Monks concerning the following question:
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...
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?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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Trouble with Tie::IxHash
by bart (Canon) on May 23, 2006 at 18:49 UTC | |
by Anonymous Monk on May 24, 2006 at 23:04 UTC | |
|
Re: Trouble with Tie::IxHash
by ikegami (Patriarch) on May 23, 2006 at 18:42 UTC | |
|
Re: Trouble with Tie::IxHash
by demerphq (Chancellor) on May 23, 2006 at 21:55 UTC | |
|
Re: Trouble with Tie::IxHash
by MonkE (Hermit) on May 23, 2006 at 18:44 UTC | |
by gellyfish (Monsignor) on May 23, 2006 at 18:47 UTC |