in reply to Maintaing the insertion order of an N-tier hash

If order matters, you have an array, not a hash. And since order matters in XML, you'll want arrays of arrays of arrays, not hashes of hashes of hashes.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

  • Comment on •Re: Maintaing the insertion order of an N-tier hash

Replies are listed 'Best First'.
Re: •Re: Maintaing the insertion order of an N-tier hash
by bean (Monk) on Feb 28, 2004 at 04:11 UTC
    If you really need a hash, you could always keep the order information in arrays in the hash like this:
    $VAR1 = { 'order' => [ 'Foo' ], 'data' => { 'Foo' => { 'order' => [ 'D', 'A', 'E', 'B', 'C' ], 'data' => { 'D' => { 'order' => [ '1' ], 'data' => { '1' => {} }, 'A' => {}, 'E' => { 'order' => [ '1', '2', '3', '4' ], 'data' => { '1' => {}, '2' => {}, '3' => {}, '4' => {} } } ...etc.
    Of course, maintaining that structure could be a chore. Java has what you're looking for - a hash that maintains order - it's called a LinkedHashMap. Basically it's a hash with a linked list running through it. <handwaving>I leave the Perl implementation as an exercise for the reader...</handwaving>

    Update

    Ok, I feel like an idiot. Ignore the Java ramblings - IxHash is the same as a LinkedHashMap. The data structure works, however (even if it is a pain).
Re: •Re: Maintaing the insertion order of an N-tier hash
by dragonchild (Archbishop) on Feb 28, 2004 at 18:52 UTC
    What I did in Excel::Template was to create a hash as the root node. It has an array (called children) of children nodes, each represented as a hash. And, so forth. Some intelligent coding in the baseclass and everything came together.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.