in reply to Array of array building

Perl doesn't have multi-dimensional arrays; it has arrays of references to arrays, so doing something like

print join("\n",@AoA); #where @AoA is an Array of Arrays
will print a list of array references.

To manipulate individual elements, the sub-array has to be dereferenced, sort of like this:

my @AoA; my @subarray; @subarray = @{$AoA[$i]}; tr/A-Z/a-z/ foreach @subarray; $AoA[$i] = \@subarray;

Note that the above is not intended to be valid Perl code, that's why I said "sort of like this." Check perlref for an explanation guaranteed to be correct. And I think it's clear, too.


Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

Replies are listed 'Best First'.
Re^2: Array of array building
by jethro (Monsignor) on Oct 01, 2008 at 17:06 UTC

    To manipulate individual elements, the sub-array has to be dereferenced

    Literally the sentence is correct (somewhere the reference must be dereferenced, even if it is implicit). But one could get a false impression. To minimize any confusion, the easiest way to manipulate individual elements is still

    $AoA[$i][$j]= $whatever;
      To make it more explicit for anyone reading the code, you can use the dereference operator between the []s thus:
      $AoA[$i]->[$j]= $whatever;
      This means access an element of @AoA, dereference it and then access an element of the list you find there.

      It's optional as far as the interpreter is concerned, but sometimes it's worth being more explicit.

      Maybe one of those times is when you need a more visible reminder that you'll need to deep copy your data structures, to follow on from GrandFather's comment.

      --
      .sig : File not found.

      Thanks for the correction.


      Information about American English usage here and here. Floating point issues? Please read this before posting. — emc