Alright, to begin, I am pretty much a Perl newbie, I only just started learning the language for a project with the help of the "Perl Cookbook" by O'Reilly that someone loaned me. This is my first post, so be kind.

My Problem is that I have created a complex (at least it seems complex to me in Perl as I am more used to C) data structure.

Basically the data structure looks like this: I have a hash table where I store the name of a server and a reference to another hash as a pair.
Within this second Hash, I store a Process ID and a reference to another Hash as a pair.
Within this third Hash, I store a FD (a column from the LSOF file) and a reference to an array as a pair.
In this array I store all the data from an LSOF line. Now I have checked this Data structure, and everything seems to be saved properly and nothing overwritten etc.

My Problem however is accessing this data with several foreach loops. Its probably best if I show you my Code:
17 foreach $server (keys %servers) #returns individual servers 18 { 19 print "$server\n"; 20 foreach $process (keys %{$servers{$server}}) #returns indi +vidual processes on a server 21 { 22 print "$process\n"; 23 foreach $processPart (keys %{$servers{$server{$pro +cess}}}) # returns part of a process 24 { 25 print "$processPart\n"; 26 } 27 } 28 }

now, this works up until the second level. It prints out all of the $server and $process values but does not print $processPart.

my problem I think is the line:

foreach $processPart (keys %{$servers{$server{$process}}})

what im trying to do here is return the keys of the the third hash (eventually I want to return the actual Array reference and later the actual data in the array, but this shouldn't be a problem once I get the past this step (I think).

Basically, how do I keep accessing deeper and deeper Nested Hashes/Arrays in a foreach in this manner?

I was also wondering if there was a better way rather than just nested foreach statements. Like I said I am new to Perl so forgive me if there is a really easy answer to this.

Looking forward to help as this has been bugging me for three days.

In reply to Nested Hash/Arrays Access by Wookimonsta

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.