element has asked for the wisdom of the Perl Monks concerning the following question:

I have a complex data structure and I'm getting lost in the syntax. The top level of the structure is an array, let's call it "objarray" (about 90 elements). Each element of "objarray" is a hash. One of the hash's keys is called "apts". The value of that key is a 2D array (let's call it "aptarray"), consisting of 2 columns and several rows. How do I access the [3][1] element of "aptarray" in the 72nd element of the top-level "objarray"? The syntax I tried produces an error: "syntax error at test.pl line 292, near "][" "

292: my $value = @{$objarray[72]{'apts'}}[3][1];

Replies are listed 'Best First'.
Re: Syntax for an array of hashes, with 2D array in hash value
by GrandFather (Saint) on Mar 08, 2011 at 04:23 UTC

    It's easier than that. Try my $value = $objarray[72]{'apts'}[3][1];.

    The thing to remeber is that Perl arrays and hashes only ever store scalar values, but a scalar can be a reference to something complicated like an array or a hash. Generally in Perl you use -> to dereference stuff, but Perl allows you to omit the -> when you have a nest of references as in the case you describe.

    The other thing to remember is that in Perl the sigil tells you what type is returned so if you want a scalar value you need to use a $ up front.

    True laziness is hard work
Re: Syntax for an array of hashes, with 2D array in hash value
by wind (Priest) on Mar 08, 2011 at 04:36 UTC
       my $value = $objarray[72]{'apts'}[3][1]

      Thanks for the simple code, it works well.

      I did read about arrays of hashes in the suggested sources, but it's not quite trivial to step up from there to "an array of hashes, each storing a 2D array".

        The top level of the structure is an array, let's call it "objarray" ...
        my @objarray = ();
        ...(about 90 elements). Each element of "objarray" is a hash.
        my @objarray = ( { }, { }, ({ }) x 87, # 87 more elements... { }, );
        One of the hash's keys is called "apts".
        my @objarray = ( { 'apts' => '???', }, { 'apts' => '???', }, ({ 'apts' => '???', }) x 87, { 'apts' => '???', }, );
        The value of that key is a 2D array ... consisting of 2 columns and several rows.
        my @objarray = ( { 'apts' => [ [11,12], [13,14], [15,16], ], }, { 'apts' => [ [21,22], [23,24], [25,26], ], }, ({ 'apts' => '???', }) x 87, { 'apts' => [ [91,92], [93,94], [95,96], ], }, );
        >perl -wMstrict -le "my @objarray = ( { 'apts' => [ [11,12], [13,14], [15,16], ], }, { 'apts' => [ [21,22], [23,24], [25,26], ], }, ({ 'apts' => '???', }) x 87, { 'apts' => [ [91,92], [93,94], [95,96], ], }, ); ;; print 'elements in array: ', scalar @objarray; print $objarray[89]{'apts'}[2][1]; " elements in array: 90 96

        (I cheated a bit on the construction of the array and so could not use the indices in the OP, but you get the idea.)

Re: Syntax for an array of hashes, with 2D array in hash value
by planetscape (Chancellor) on Mar 09, 2011 at 00:57 UTC