This is mostly academic, since I can make this work in another way, but I know I've done this before in a different way and I can't remember what I used - and it's driving me nuts!

I haven't been using Perl for a while, other than a few scripts here and there when I needed to throw something small together, so I'm out of touch. I'm using a hash of hashes. I know how to get to the elements in the nested hash, but I'm trying to get the keys for that nested hash instead.

I have this:

$form{$field} = \%field; ... foreach $key1 (keys %form) { print "Key: $key1\n"; $t1 = ${form{$key1}}; foreach $key2 (keys %$t1) { print "\tName: $key2, Value: $$t1{$key2}\n"; #Same results - testing different format print "\tName: $key2, Value: $form{$key1}{$key2}\n"; } }

I've done this before without a temporary variable like $t1. It's just nagging me - I can't remember the format I can use so I can get the keys from the nested hash without having to use a temporary variable.

I've found a number of pages with material on hashes of hashes, but they tend to assume you know all the keys and won't be trying to get them to iterate through them.

So how could I get the keys from the nested hash without an interim variable?


In reply to Getting Keys From Hash In a Hash by HalNineThousand

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.