2

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?

1
  • 2
    Why do you need to make an array of arrays of hashes? Sometimes if you tell us what you have to model we can help you figure out more specific examples (or if an AoAoH is right for that). Commented Sep 9, 2010 at 19:58

4 Answers 4

6

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

Sign up to request clarification or add additional context in comments.

Comments

6

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.

2 Comments

This feature of Perl is known as autovivification.
Please don't use the variable $a (or $b) in examples that don't involve sort. $a and $b are special variables in Perl and can cause odd errors when used outside of a function passed to sort.
4
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'

3 Comments

Or $aah->[0][1]{bar}... everything in an array or hash is a scalar, thus any substructures can be assumed to be references, thus only the first arrow is ever needed.
Or $$arr[0][1]{bar}. I personally prefer that over the -> notation.
This doesn't need to be an array reference, replace the first [ with ( the last ] with ) and $aah with @aah. Now you can access the data like this: $aah[0][1]{bar}; no de-referencing syntax needed, for the reason stated by Axeman.
0
my $arr = 
  [
    [
      {key1 => $value1, key2 => $value2},
      {key1 => $value3}
    ],
    [
      {rubbish => 'nonsense'},
    ]
   ];

etc.

3 Comments

Just because its a nested data structure doesn't mean it needs to be an arrayref, it can be an array of arrayrefs of hashrefs.
@MkV: Most programmers would prefer the arrayref, since it's cheaper and simpler to pass around as a data structure than a list would be. BTW, did you downvote this answer? If so, that's a pretty lame reason to do so.
Of course it can be an array rather than an arrayref at the top. So what? It doesn't change the answer in any significant way. I don't know why you felt it necessary to add that comment to my and jkramer's answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.