Tie:IxHash simply adds one feature to hashes; it keeps track of the order in which the keys are inserted into the hash, allowing you to get the keys out in the same order. Everything else is just like a regular hash, whether the values are references or plain old scalars.

Note that if you wanted something like an order hash of ordered hashes, you would have to tie the outermost hash and each of the inner hashes to Tie::IxHash. If only the outermost hash needs to be ordered, you only need to tie the outermost hash, of course.

Update: Here's a quick LoH example, with ordered hashes.

use Tie::IxHash; my @Loh; for (1..5) { my %hash; tie %hash, 'Tie::IxHash'; push @LoH, \%hash; # a separate instance of the lexical %hash # is created each time through the loop }
@LoH now contains references to 5 ordered hashes. You can treat this structure exactly like an ordinary list of hashes; plus you can use the special features of a Tie::IxHash hash whenever you want.
for (qw/Just another Perl hacker/) { $LoH[0]{$_} = $_; } print join(' ', keys %{ $LoH[0] }), "\n";
This will print 'Just another Perl hacker', because Tie::IxHash returns the keys in the same order they were created. Yay!

Many thanks to tilly for help working out a frustrating problem I ran into with my example. Upshot: do not put the tied reference itself (the return value from tie) in the data structure! Instead, tie the hash first, and put a plain old reference to the hash in the data structure.

Update: Tie::IxHash should be quoted in the call to tie. Thanks ybiC!


In reply to Re: Can Tie::IxHash be used to order elements in a list or hash of hashes? by chipmunk
in thread Can Tie::IxHash be used to order elements in a list or hash of hashes? by ybiC

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.