Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Nested Data Structures for Dummies?

by GrandFather (Sage)
on Apr 15, 2010 at 00:13 UTC ( #834801=note: print w/replies, xml ) Need Help??

in reply to Nested Data Structures for Dummies?

Where are you having trouble? With many sorts of problem I find breaking things up into smaller chunks helps a lot. I'll bet you already use that technique for managing code using subroutines and modules for example. So how can you do that for data? One way that can help is to create temporary variables that provide access to the current part of a structure that you are interested in. Consider:

sub itemPrice { my ($self, $itemName) = @_; my $item = $self->{invent}{current}{items}{$itemName}; return $item->{price}; }

Ok, no great advantage in such a trivial case, but if you needed to access $item multiple times the virtue is obvious. Very often such a technique is valuable in a loop where you are iterating over items but accessing the item is somewhat indirect.

In most cases once you have a grasp of the general principals for managing references the rest of the details are very specific to your actual data structure and application. Some times a recursive technique does the magic, other times using a temporary reference as suggested above is the trick. Some times wrapping the access up in a sub may be the best medicine.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: Nested Data Structures for Dummies?
by jedikaiti (Hermit) on Apr 15, 2010 at 00:52 UTC

    I just can't get the lightbulb in my head to go off, essentially. I think it's when I'm iterating through a HoHoWhatever, and I get confused with whether it's giving me actual data or a reference, and how to handle each. And no matter how many times I go through one or another, the logic just won't click. It's driving me nuts!

    Swiss Army Nerd

      The key thing to remember is that you can only store a scalar as a data structure value. So in the context of an array each element is a scalar - the element's value may reference something more interesting like another array, but the value is a scalar.

      You may find it helps to assign the reference to a temporary variable with a suitable name even in trivial cases so that you make it clear to yourself that what you are dealing with is a reference. Only the very innermost element can be a non-reference scalar value (a string or number for example).

      It may help to show us some of the code and the data structures that give you grief so we have a concrete example to work with that applies to what you are using.

      True laziness is hard work
        How about this?
        %hash = { 'some item' = > { 'source' => 'a source', 'conversions' => [ { 'conversion 1' => 'value 1', 'conversion 2' => 'value 2' }, { 'conversion 3' => 'value 3', 'conversion 4' => 'value 4' 'conversion 5' => 'value 5' } ], 'whatever' => 'eh' }, 'another item' = > { 'source' => 'another source', 'conversions' => [ { 'conversion 1' => 'value 1', 'conversion 2' => 'value 2' } ], 'whatever' => 'eh' }, 'one more item' = > { 'source' => 'another source', 'whatever' => 'eh' }, }
        Swiss Army Nerd

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2018-10-20 13:40 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (117 votes). Check out past polls.