Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

How do I tie an array of hashes?

by Anonymous Monk
on Sep 21, 2000 at 03:18 UTC ( #33428=categorized question: print w/replies, xml ) Need Help??
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 read these before you post! —
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (5)
    As of 2020-02-20 01:00 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (86 votes). Check out past polls.