I create a hash from a file using format similar to /etc/group:

process:text:action:email

ie:

syslogd:syslogd daemon::user@host.com

I create hash:

@fields = split /:/; $process = shift @fields; $hash{$process}{'TEXT'} = shift @fields; $hash{$process}{'ACTION'} = shift @fields; $hash{$process}{'EMAIL'} = shift @fields;

And I can do:

print "$hash{'syslogd'}{'EMAIL'}\n"; and get "user@host.com"

But how do I reference not the key ($process) but the sub-key (for lack of better term) in the above hash in a foreach?

foreach my $processKey (sort keys %hash) { print "$_"; foreach my $hashKey (sort keys $hash{$processKey}) { print ":$daemontable{$processKey}{$hashKey}\n"; } }

This ofcourse doesn't work. I don't understand how to get at the "EMAIL", "ACTION", "TEXT" fields of a hash other then specifying them manually (which defeats the purpose)

The idea is that I don't need to manually add code everytime I add a new sub-key into the foreach loop.

I apologize if my terminology is off.


In reply to Referencing dynamic hash elements by Anonymous Monk

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.