开发者

Array of Hashes in Hash of Hashes?

I开发者_开发知识库 have this HoH

#!/usr/bin/perl

use warnings;
use strict;

my $a = {
    '0' => {
            'i' => -1,
            'u'  => -1,
    },
};

But what I would like is

my $a = {
    '0' => {
            'i' => -1,
        'u'  => -1,
            (
              {
               'i' => -1,
               't' => -1,
              },
            ),
          },
        };

which gives an error.

Is it not possible in have an AoH in a HoH?


It's probably yelling you "Odd number of elements in anonymous hash at $filename line $line", right? That's because you can't really just stuff an array into a hash by itself -- the array ref will need to be keyed, just like any other hash element. Also, you will need to use [] instead of () to make an array ref:

my $a = {
    0 => {
        i => -1,
        u => -1,
        x => [{i => -1, t => -1}],
    },
};

produces no errors. Then you can access into it like so: $a->{0}{x}[0]{i};


You should have a key before your array :

my $a = {
    '0' => {
            'i' => -1,
            'u'  => -1,
            'a' => [
      # here ^ is the key
              {
               'i' => -1,
               't' => -1,
              },
            ],
          },
        };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜