LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl - How to store and access an array in a hash of hashes? (https://www.linuxquestions.org/questions/programming-9/perl-how-to-store-and-access-an-array-in-a-hash-of-hashes-818467/)

DevonB 07-07-2010 06:08 AM

Perl - How to store and access an array in a hash of hashes?
 
I am in need of some syntax help. I'm trying to figure out how to store and retrieve an array out of a hash of hashes. For this example, I'm trying to access the city list for a particular state for a particular country. I understand I could do a join and split on the hash key to combine Country and State, but trying to keep things separated.

The code I have gets in all the information for the Countries, and states, and gets the list of cities together, no problem. I store all the city names in an array, then make an anonymous pointer to the array for the hash, like this -

$MY_CITIES{$COUNTRY}{$STATE} = [@CITIES]

I believe that syntax is correct, or is it? What I'd like to do is I need to cycle through every hash to find if a city exists or not. If it exists in 3 states, then it should print 3 times. Here is the code block to search -

Code:

for $COUNTRY (@LIST_OF_COUNTRIES) {
  for $STATE ( keys %MY_CITIES{$COUNTRY){$STATE} ) {
    for $INDEX ( 0..$#MY_CITIES{$COUNTRY}{$STATE} ) <- Is that right?
      if ( ${$MY_CITIES}{$COUNTRY}{$STATE}[$INDEX] =~ /$SELECTED_CITY/ ) {
        print "Found the city of $SELECTED_CITY in the state of $STATE in the country of $COUNTRY\n";
      }
    }
  }
}

My questions are thus -

1.) Is my assignment of $MY_CITIES{$COUNTRY}{$STATE} = [@CITIES] right to assign the cities to that hash of hashes key?

2.) My second loop line with for $STATE, how to I walk all the State keys within the $MY_CITIES{$COUNTRY}{$STATE} hash? keys %MY_CITIES{$COUNTRY}{$STATE} like that?

3.) The if statement that does the search for the city in the anonymous stored array, is that written correctly? ${$MY_CITIES}{$COUNTRY}{$STATE}?

Thanks for the help!

Devon

Sergei Steshenko 07-07-2010 06:26 AM

Quote:

Originally Posted by DevonB (Post 4026107)
I am in need of some syntax help
...

'man perl' shows:

Code:

    1 PERL(1)                                                            Perl Programmers Reference Guide                                                          PERL
      1 (1)
      2
      3
      4
      5 NAME
      6        perl - Practical Extraction and Report Language
      7
      8 SYNOPSIS
      9        perl [ -sTtuUWX ]      [ -hv ] [ -V[:configvar] ]      [ -cw ] [ -d[t][:debugger] ] [ -D[number/list]]
    10            [ -pna ] [ -Fpattern ] [ -l[octal] ] [ -0[octal/hexadecimal] ]      [ -Idir ] [ -m[-]module ] [ -M[-]'module...' ] [ -f ]      [ -C [number/list]]
    11            [ -P ]      [ -S ]      [ -x[dir] ]      [ -i[extension] ]      [ -e 'command' ] [ -- ] [ programfile ] [ argument ]...
    12
    13        If you're new to Perl, you should start with perlintro, which is a general intro for beginners and provides some background to help you navigate the rest o
    13 f
    14        Perl's extensive documentation.
    15
    16        For ease of access, the Perl manual has been split up into several sections.
    17
    18        Overview
    19
    20            perl                Perl overview (this section)
    21            perlintro          Perl introduction for beginners
    22            perltoc            Perl documentation table of contents
    23
    24        Tutorials
    25
    26            perlreftut          Perl references short introduction
    27            perldsc            Perl data structures intro
    28            perllol            Perl data structures: arrays of arrays

...
    58            perldata            Perl data structures

- read the items in red.

zirias 07-07-2010 06:40 AM

And a quick tip: Instead of dereferencing a lot with ${${$[ etc, use the "dereferencing-member-access-operator" ->

like $foo->{'bar'}->[42]

(here $foo is a hash ref and the element 'bar' in the hash is an array ref)

much better readable...

DevonB 07-07-2010 07:32 AM

I got it finally. The HoH section in man perldsc got me on the right track, and some fine guessing on my part finally gelled it.

The whole ${${$ thing, in some examples I agree with, and in others, I find it easier with the ${${$ etc way. Multi-layer deep, absolutely. At the third dimension, it's not very difficult, and reads more cleanly with the ${$ methodology, to me.

Devon

DevonB 07-07-2010 07:57 AM

Quote:

Originally Posted by DevonB (Post 4026107)
My questions are thus -

1.) Is my assignment of $MY_CITIES{$COUNTRY}{$STATE} = [@CITIES] right to assign the cities to that hash of hashes key?

2.) My second loop line with for $STATE, how to I walk all the State keys within the $MY_CITIES{$COUNTRY}{$STATE} hash? keys %MY_CITIES{$COUNTRY}{$STATE} like that?

3.) The if statement that does the search for the city in the anonymous stored array, is that written correctly? ${$MY_CITIES}{$COUNTRY}{$STATE}?

Thanks for the help!

Devon

To answer my own question, for future reference -

1. Yes, correct.

2. No. It should be keys %{ $MY_CITIES{$COUNTRY} }

3. No. It should be $MY_CITIES{$COUNTRY}{$STATE}[$INDEX]

Also, the Index line has an error as well. It should be $#{$MY_CITIES{$COUNTRY}{$STATE}}

Devon


All times are GMT -5. The time now is 10:09 PM.