Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How to get my hash data using foreach loop??

by Ratazong (Monsignor)
on Apr 11, 2014 at 12:59 UTC ( [id://1081957]=note: print w/replies, xml ) Need Help??


in reply to How to get my hash data using foreach loop??

Hi mak.jordan!

Here are few observations on your code:

1. Looking at

$PG{$3}{"$1-$2"}{$cou} = $4;
you have three levels of keys in your hash. Therefore you only need three loops for accessing your data. Your innermost data ($4) is the value, not a key. So you should get rid of the innermost foreach loop.

2. A hint on debugging: it is much easier to develop the access of the hash step by step: First only write the outmost loop and check if the keys are teh ones that you are expecting. In the second step you add the second level, and check if the loop is working as expected (with a print statement) and so on. Then you'll find out where your code is wrong.

3. Having done this, you'll probably notice an error in the following line:

foreach $path3 ( keys %{$PG{$path1{$path2}}} )
Using $PG{$path1{$path2}}, you try to use $path1 as a hash, using the key $path2. And use the value of that hash-lookup as first-level-key in PG. However you want to access the second level of the $PG-hash. That would be done with $PG{$path1}{$path2}

HTH, Rata

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 22:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found