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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Nested Data Structures for Dummies?
by jedikaiti (Hermit) on Apr 15, 2010 at 00:52 UTC | |
by GrandFather (Saint) on Apr 15, 2010 at 01:12 UTC | |
by jedikaiti (Hermit) on Apr 15, 2010 at 02:50 UTC | |
by GrandFather (Saint) on Apr 15, 2010 at 04:17 UTC | |
by jedikaiti (Hermit) on Apr 15, 2010 at 16:31 UTC |