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

Re: What data structure is this and how to access?

by roboticus (Chancellor)
on Jan 18, 2012 at 11:38 UTC ( [id://948522]=note: print w/replies, xml ) Need Help??


in reply to What data structure is this and how to access?

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.

Replies are listed 'Best First'.
Re^2: What data structure is this and how to access?
by tobyink (Canon) on Jan 18, 2012 at 12:48 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://948522]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-19 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found