I need to nest an anonymous hash and an anonymous array inside a hash, and beg help from the wisdom of the monastery to supplement my weak understanding. The data to be managed is like this (almost the same as the windows registry, fwiw) plus the line of (wrong) code to store it:
item 1 # as read from file $key = "k1"; $valname = "vname1"; $value = "v1"; $type = "t1"; $flag = "f1"; $hash{ $key } = ( { $valname => [ $value, $type, $flag ] }); item 2 $key = "k1"; $valname = "vname2"; $value = "v2"; $type = "t2"; $flag = "f2"; $hash{ $key } = ( { $valname => [ $value, $type, $flag ] }); item 3 $key = "k3"; $valname = "vname3"; $value = "v3"; $type = "t3"; $flag = "f3"; $hash{ $key } = ( { $valname => [ $value, $type, $flag ] }); item N (indeterminate until runtime)
when stored it should be something like this:
k1 => vname1 => (v1, t1, f1) vname2 => ( v2, t2, f2 ) k3 => vname3 => ( v3, t3, f3 )
Note that both items 1 and 2 have the same value for $key. My attempt at storing the data is wrong, so item2 overwrites item1 and k1's inner (anonymous) hash has only item2's data.
foreach $i (keys %hash) { print "i=$i\n"; foreach $j (keys %{$hash{$i}} ) { @content = $hash{$i}->{$j}; #print "\tvalname=$j content=$$hash{$i}->{$j}[1] \n"; print "\tj=$j val=$hash{$i}->{$j}[0] type=$hash{$i}->{$j}[1] +flag=$hash{$i}->{$j}[2] \n"; print "$content[0] $content[1] $content[2]\n"; } }
The loop to access the data isn't good either -- somewhere the perl's too slippery for me -- so any hints or even better solutions would be wonderful.
Thanks!

In reply to inner anonymous hash by anadem

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.