Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Nested Data Structures for Dummies?

by ikegami (Pope)
on Apr 15, 2010 at 00:28 UTC ( #834802=note: print w/ replies, xml ) Need Help??


in reply to Nested Data Structures for Dummies?

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}; ... } }


Comment on Re: Nested Data Structures for Dummies?
Select or Download Code
Re^2: Nested Data Structures for Dummies?
by jedikaiti (Friar) on Apr 15, 2010 at 00:59 UTC

    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;

        Some days, it's hard to make myself do that. Thank you.

        Kaiti
        Swiss Army Nerd

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2014-07-25 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (172 votes), past polls