How do I create a hash of hashes in Perl?

回眸只為那壹抹淺笑 提交于 2019-12-01 16:38:11
Alexandr Ciornii
  1. You should always use "use strict;" in your program.

  2. Use references and anonymous hashes.

use strict;use warnings;
my %a;

my %b;
$b{str} = "hello";  
$a{1}={%b};

%b=();
$b{str} = "world";
$a{2}={%b};

print "$a{1}{str}  $a{2}{str}";

{%b} creates reference to copy of hash %b. You need copy here because you empty it later.

Hashes of hashes are tricky to get right the first time. In this case

$a{1} = { %b };
...
$a{2} = { %b };

will get you where you want to go.

See perldoc perllol for the gory details about two-dimensional data structures in Perl.

Short answer: hash keys can only be associated with a scalar, not a hash. To do what you want, you need to use references.

Rather than re-hash (heh) how to create multi-level data structures, I suggest you read perlreftut. perlref is more complete, but it's a bit overwhelming at first.

Mike, Alexandr's is the right answer.

Also a tip. If you are just learning hashes perl has a module called Data::Dumper that can pretty-print your data structures for you, which is really handy when you'd like to check what values your data structures have.

use Data::Dumper;
print Dumper(\%a); 

when you print this it shows

$VAR1 = {
          '1' => {
                   'str' => 'hello'
                 },
          '2' => {
                   'str' => 'world'
                 }
        };

Perl likes to flatten your data structures. That's often a good thing...for example, (@options, "another option", "yet another") ends up as one list.

If you really mean to have one structure inside another, the inner structure needs to be a reference. Like so:

%a{1} = { %b };  

The braces denote a hash, which you're filling with values from %b, and getting back as a reference rather than a straight hash.

You could also say

$a{1} = \%b;   

but that makes changes to %b change $a{1} as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!