Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Nested Data Structures for Dummies?

by jedikaiti (Friar)
on Apr 14, 2010 at 23:32 UTC ( #834795=perlquestion: print w/ replies, xml ) Need Help??
jedikaiti has asked for the wisdom of the Perl Monks concerning the following question:

I posted this in the CB earlier, but thought I'd ask here too, and get input from those who weren't around then.

Do you have a favorite "Accessing Data in Nested Structures for Complete Idiots" tutorial that you could point me to? I seem to just not quite be getting that, and rather than posting sample data structures from hell and asking for help ad nauseam, I thought I'd find what some of the best sources of explanations are. Unconventional explanations welcome.

Kaiti
Swiss Army Nerd

Comment on Nested Data Structures for Dummies?
Re: Nested Data Structures for Dummies?
by blokhead (Monsignor) on Apr 14, 2010 at 23:44 UTC

      Checking them now - thanks!

      Kaiti
      Swiss Army Nerd
Re: Nested Data Structures for Dummies?
by GrandFather (Cardinal) on Apr 15, 2010 at 00:13 UTC

    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

      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!

      Kaiti
      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
Re: Nested Data Structures for Dummies?
by ikegami (Pope) on Apr 15, 2010 at 00:28 UTC

    Do you have a favorite "Accessing Data in Nested Structures for Complete Idiots" tutorial

    Bad question. Don't access data in a nested structure. Access the data you have at hand.

    One would access the countries in a list of counties as follows:

    for my $country_name (keys(%$countries)) { my $country = $countries->{$country_name}; ... }

    One would access the provinces in a list of provinces as follows:

    for my $province_name (keys(%$provinces)) { my $province = $provinces->{$province_name}; ... }

    Maybe $provinces is the same as $country or $country->{provinces} or $country->get_provinces(). Does it matter? No.

    for my $country_name (keys(%$countries)) { my $country = $countries->{$country_name}; my $provinces = $country->{provinces}; for my $province_name (keys(%$provinces)) { my $province = $provinces->{$province_name}; ... } }

      So what if you have a list of countries, where each country has a list of provinces, and each province has a list of cities, and some cities have lists of neighborhoods? But you only need neighborhoods with names beginning with "k" in provinces that begin with "c". This is where I start getting confused and looking for a stiff drink.

      Kaiti
      Swiss Army Nerd

        You did exactly what I suggested you don't do. Don't look at the whole. These are the things you actually need to do:

        • Identify if a neighbourhood name starts with 'k'.
        • Get a city's list of neighbourhoods.
        • Get a province's list of cities.
        • Identify if a province's name starts with 'c'.
        • Get a country's list of provinces.
        • Get a list of countries.

        They're all independent. Nothing in there even suggests that you have a nested structure in memory.

        The hard part:

        print("$country_name/$province_name/$city_name/$neighbourhood_name\n") if $neighbourhood_name =~ /^k/i;
Re: Nested Data Structures for Dummies?
by fullermd (Curate) on Apr 15, 2010 at 04:35 UTC
    Do you have a favorite "Accessing Data in Nested Structures for Complete Idiots" tutorial that you could point me to?

    Not as such, no. I wrote up a post some time back on a related question, but it's mostly about syntax, not the semantics. At one level, of course, it's simple; unless you're dealing with a "top level", you've always got a reference until you dereference it (implicitly or explicitly). But presumably your troubles at somewhat beyond that?

    However, ikegami's post is cogent. Think locally; you never deal with a giant data structure. You deal with at most a slice of a data structure, and usually a piece of data. If you can answer the questions "what piece of data do I want" and "what do I want to do with it", it's usually a lot easier to figure out how to go about it.

Re: Nested Data Structures for Dummies?
by scorpio17 (Monsignor) on Apr 15, 2010 at 14:44 UTC

    You've got a copy of the Camel book (Programming Perl), right? Try Chapter 4: References and Nested Data Structures - look for the section 'Data Structure Code Examples'. That should help, I hope.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://834795]
Approved by GrandFather
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2014-09-18 23:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (126 votes), past polls