Contributed by Anonymous Monk on Sep 21, 2000 at 03:18 UTC
Q&A  > Data Structures


I have an array of hashes:
push @data { yo => mama, bliz => at }
Can I tie the hashes to retrieve their values in insertion order?

Answer: How do I tie an array of hashes?
contributed by Fastolfe

The items in your array are in insertion order, so what remains is each individual hash. In your example above, you have a list with one element: a standard hashref. Since you can't "tie" an anonymous reference like this, you would need to use the object-oriented abilities of Tie::IxHash instead:

use Tie::IxHash; push @data, Tie::IxHash->new( yo => mama, bliz => at ); push @data, Tie::IxHash->new( one => two, three => four); print join(", ", keys %{$data[1]}); # one, three
You can also create separate hashes, tied to Tie::IxHash (per the man page) and push references to each of those tied variables onto @data.

Please (register and) log in if you wish to add an answer

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