in reply to Hash in a hash value
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Hash in a hash value
by Anonymous Monk on Jun 03, 2003 at 18:15 UTC |