Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I don't know how this is formaly named/described
push @TestVals, { A => $var1, B => $var2, C => $var3, D => $var4 } foreach my $row (@TestVals){ print Dumper $row; }
Script results
$VAR1 = { 'A' => 'TEST DATA 1', 'B' => 'TEST DATA 2', 'C' => 'TEST DATA 3', 'D' => 'TEST DATA 4', };
What is this type of struture called? Array of hash? How do I access each item within $row? Like $row->A? Thanks

Replies are listed 'Best First'.
Re: What data structure is this and how to access?
by roboticus (Chancellor) on Jan 18, 2012 at 11:38 UTC

    Yes, it's an array of hashes (often abbreviated to AoH). You can access items like $TestVals[0]{B}. Read perldoc perldsc for more information about building data structures.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I'm not a fan of such simplifications. They obscure the true nature of the beast.

      Calling it an "array of hashes" suggests that doing this will fail...

      push @TestVals, "Monkey";

      ... because you're pushing something that is not a hash onto a supposed "array of hashes".

      @TestVals is simply an array. It just so happens that the only item the original asker pushed onto the array happened to be a reference to a hash. But that doesn't necessarily mean that everything on @TestVals will be a hashref.

        tobyink:

        I understand what you mean. However, sometimes you have to start with a simplification to get the basic point across without complicating the environment overmuch. When we answer a question, we're frequently (consciously or not) walking a tightrope trying to balance between a simple (and somewhat deficient) answer and a longer (and more technically accurate, yet possibly more confusing) answer. External factors also come into play: for example, my time was short this morning before my commute to work, so it biased me towards shorter answers.

        In this specific case, he wanted to know the name of the structure he was creating as well as how to access the elements. So for the question asked, I think my answer is fine. Obviously, a longer answer showing the flexibility of the array, like you demonstrate, would also be fine. I just opted for the shorter answer in this case. If he posed the question differently, I might've presented an answer more like yours.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        I wouldn't call that a simplification. It really is a AoH as long as he doesn't also push a scalar or a non-hash-pointer.

        In the same way I can call a hash where the elements are linked in binary tree form a binary tree.

Re: What data structure is this and how to access?
by planetscape (Chancellor) on Jan 18, 2012 at 12:14 UTC
Re: What data structure is this and how to access?
by JavaFan (Canon) on Jan 18, 2012 at 14:10 UTC
    Work from the inside out.

    To access a hash element, you'd write $hash{key}. But you may replace the name of the hash with a block that returns a reference to a hash: ${$hashref}{key}. In this case, the first element of @TestVals is a reference to a hash, so we get: ${$TestVals[0]}{key}. Filling in the key gets you: ${$TestVals[0]}{A}.

    Now, optionally, you may want to use some shortcuts. ${EXPRESSION}{key} may also be written as (EXPRESSION)->{key}, so, in your case, ($TestVals[0])->{A}. The parens aren't necessary in this case, leading to $TestVals[0]->{A}. Finally, there's a second short cut. Whenever you have [INDEX]->[INDEX], [INDEX]->{INDEX}, {INDEX}->[INDEX], or {INDEX}{INDEX}, you may drop the arrow. This leads to $TestVals[0]{A}.

    But do keep in mind that $TestVals[0]{A} is a shortcut for the formal ${$TestVals[0]}{A}. (I always find it funny that neither the shortcut, nor the formal way use an arrow, but the intermediate form does).

Re: What data structure is this and how to access?
by Anonymous Monk on Jan 18, 2012 at 11:40 UTC