Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
We don't bite newbies here... much
 
PerlMonks  

Trouble with Tie::IxHash

by sara2005 (Scribe)
on May 23, 2006 at 18:22 UTC ( #551214=perlquestion: print w/ replies, xml ) Need Help??
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...

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?

Comment on Trouble with Tie::IxHash
Select or Download Code
Re: Trouble with Tie::IxHash
by ikegami (Pope) on May 23, 2006 at 18:42 UTC
    Change
    $hnr{'MAIN'} = { %list1 }; $hnr{'MAIN'}{'ZOP'} = { %list2 }; $hnr{'MAIN'}{'AP'} = { %list2 };
    to
    $hnr{'MAIN'} = \%list1; $hnr{'MAIN'}{'ZOP'} = \%list2; $hnr{'MAIN'}{'AP'} = \%list3;

    { %tied } creates a new (unordered) hash with a copy of %tied contents. That's not what you want,

    Update: Alternatively, I think tying anon hashes is perfectly fine:

    use Data::Dumper qw( Dumper ); use Tie::IxHash (); tie my %hnr, 'Tie::IxHash'; %hnr = ( ZOP => {}, AP => {}, Exit => undef ); tie %{$hnr{'MAIN'}{'ZOP'}}, 'Tie::IxHash'; tie %{$hnr{'MAIN'}{'AP'} }, 'Tie::IxHash'; my @zop_and_ap_init = ( Dev => undef, Con => undef, Test => undef, Exit => undef, New => undef, ); %{$hnr{'MAIN'}{'ZOP'}} = @zop_and_ap_init; %{$hnr{'MAIN'}{'AP'} } = @zop_and_ap_init; print Dumper(\%hnr);
Re: Trouble with Tie::IxHash
by MonkE (Hermit) on May 23, 2006 at 18:44 UTC
    Hashes have no guaranteed order. What you need is an array.

    Update: Tie:IxHash does provide guaranteed order. Sorry for overlooking that.

      Er, quite, but that is why the OP is using Tie::IxHash which does give a guaranteed order.

      /J\

Re: Trouble with Tie::IxHash
by bart (Canon) on May 23, 2006 at 18:49 UTC
    You're not creating the nested hash in the proper order, as you're using anonymous hashes as values somewhere, which are intrinsically unordered. And then, it's too late to fix it.

    First off, take a look at a module our co-monk japhy wrote: Tie::Autotie. It'll automatically tie deeper hashes also. If you then would assign the values one by one, it would already work:

    use Tie::Autotie 'Tie::IxHash'; tie %hash_ref, 'Tie::IxHash'; # yuck, that variable's name! $hash_ref{MAIN}{ZOP}{Dev} = undef; $hash_ref{MAIN}{ZOP}{Con} = undef; ... # etc

    You might not like the required syntax — though for reading from a data file, it'd probably work fine. I'm thinking of an alternative to anonymous hashes: tied hash references. I don't know if any module already implements them, but you can build them by hand:

    sub ixhash { tie my(%hash), 'Tie::IxHash'; %hash = @_; return \%hash; }
    In that case, you should be able to write (including the above sub):
    use Tie::IxHash; tie %hash_ref, 'Tie::IxHash'; %hash_ref = ( 'MAIN' => ixhash( 'ZOP' => ixhash( 'Dev', undef, 'Con', undef, 'Test', undef, 'Exit', undef, 'New', undef ), 'AP' => ixhash( 'Dev', undef, 'Con', undef, 'Test', undef, 'Exit', undef, 'New', undef ), 'Exit' => undef, ) );
    which looks acceptable to me... no?

    update n.b. You don't need to use Tie::Autotie if you use the latter suggestion, make sure to replace every anonymous hash with a call to ixhash(), and never rely on autovivification.

    p.s. You can use Tie::Hash::Indexed as a plug-in replacement for Tie::IxHash. The former is written in XS, the latter in Pure Perl, so it should be faster. I haven't run a benchmark, though.

      I think your suggestion will perfectly suit my requirement but it seems that Autotie will only for perl version 5.8.3 or more. Unfortunately, I have only 5.6.1 and don't have authorization to update perl version in my system.

      Is there a workaround to use this somehow?

      Appreciate your help...

Re: Trouble with Tie::IxHash
by demerphq (Chancellor) on May 23, 2006 at 21:55 UTC

    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...

    Ok, the question is why do you want to use Tie::IxHash. If you want to build an ordered structure why dont you use something like nested arrays of name=>value pairs.

    $var=[ '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, + ] ];

    Unless you have some call to address these things by their names you dont need to use a hash at all. You can iterate over such a structure pretty much as as easily as with a HoH, and preserving the order read ceases to be an issue.

    ---
    $world=~s/war/peace/g

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://551214]
Approved by ww
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2014-04-23 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (556 votes), past polls