开发者

How do I make a Perl "array of arrays of hashes"?

开发者 https://www.devze.com 2023-01-15 17:06 出处:网络
I think what I need is an Array of Array of Hash, but I have no idea how to make that. Can Perl do that?

I think what I need is an Array of Array of Hash, but I have no idea how to make that.

Can Perl do that?

And if so, how would the code look like?开发者_StackOverflow中文版


perldoc perldsc is a good document to read to get an idea of data structures in Perl.


You can address elements of such a data structure thus:

$x->[1][2]{foo} = "hello"

You don't even have to pre-define the structure. Just start working with the elements as if they're already there.


my $aah =
        [ # outer array
                [ # first inner array
                        { # first inner hash
                                foo => 'bar',
                        },
                        { # second inner hash
                                bar => 'baaz',
                        },
                ],
                [ # secnd inner array
                        #...
                ],
                # ...
        ];

You can access the elements like this:

$aah->[0]->[1]->{bar} # => 'baaz'


my $arr = 
  [
    [
      {key1 => $value1, key2 => $value2},
      {key1 => $value3}
    ],
    [
      {rubbish => 'nonsense'},
    ]
   ];

etc.

0

精彩评论

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