LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-07-2010, 06:08 AM   #1
DevonB
LQ Newbie
 
Registered: Dec 2009
Posts: 27

Rep: Reputation: 1
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
 
Old 07-07-2010, 06:26 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by DevonB View Post
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.
 
Old 07-07-2010, 06:40 AM   #3
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
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...
 
Old 07-07-2010, 07:32 AM   #4
DevonB
LQ Newbie
 
Registered: Dec 2009
Posts: 27

Original Poster
Rep: Reputation: 1
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
 
Old 07-07-2010, 07:57 AM   #5
DevonB
LQ Newbie
 
Registered: Dec 2009
Posts: 27

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by DevonB View Post
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

Last edited by DevonB; 07-07-2010 at 08:10 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl - problems creating an array of hashes Lordandmaker Programming 9 01-22-2009 09:36 AM
how to access just some part of array in perl? hottdogg Programming 9 03-25-2008 09:02 PM
Perl hash of hashes props666999 Programming 2 09-07-2006 04:43 AM
Using hash value as key for other hash in Perl scuzzman Programming 6 02-14-2006 05:08 PM
Passing hash of hashes as a parameter (Perl) rose_bud4201 Programming 8 04-21-2005 07:18 PM


All times are GMT -5. The time now is 11:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration