I'm not clear on whether you want all the lines following the 48 key to be in the referenced hash? If so, you need to change the contents of the if statement like this:

if ($key == 48) { my %msg48; while(<FH>){ chomp; ($key48, $val48) = split /=>/; $msg48{$key48} = $val48; } $msg{$key}=\%msg48; } else{ $msg{$key} = $val; }

Notice how the $msg{$key} assignment had to move inside the else clause at the end there. This is because you want to assign a reference to key 48 but just assign $val every other time. Another option would be to lose the else and just assign the reference to the hash to val:

#Change: $msg{$key}=\%msg48; #to $val=\%msg48; #And lose the else around: $msg{$key} = $val;

You get the exact same result that way with a little less fuss, although you could argue that is a little more difficult to read. Finally throw a:

use Data:Dumper;

at the top of your code and then you can print your hashes using the Dumper function which is a lot prettier than forcing it to an array!

print Dumper(\%msg);

Notice that Data::Dumper expects references to variables.

HTH

Addendum: If you want only the next line of your data file to be included in the hash basically remove the while from inside the if statement:

if ($key == 48) { my %msg; $_=<FH>; chomp; ($key48, $val48) = split /=>/; $msg48{$key48} = $val48; $val=\%msg48; }

In reply to Re: Hash in a hash value by pzbagel
in thread Hash in a hash value by djbiv

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.