Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Preserving order for nested hashes using Tie::IxHash

by shemp (Deacon)
on Aug 19, 2005 at 21:16 UTC ( [id://485308]=note: print w/replies, xml ) Need Help??


in reply to Preserving order for nested hashes using Tie::IxHash

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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://485308]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found