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

I am having trouble creating and accessing an array of arrays.

What is the best way to access one array within an array?

What is the best way to manipulate (add, modify, delete) a scalar value within an array?

I keep getting pointers to anonymous arrays instead of the arrays or scalar values themselves.

Replies are listed 'Best First'.
Re: Array of array building
by Corion (Patriarch) on Oct 01, 2008 at 16:16 UTC
Re: Array of array building
by toolic (Bishop) on Oct 01, 2008 at 16:24 UTC
    What is the best way to access one array within an array?
    De-reference it:
    use strict; use warnings; use Data::Dumper; my @aoa = ([0..3], [10..12]); # array-of-arrays print Dumper(\@aoa); for my $aref (@aoa) { my @arr = @{ $aref }; # derefernce each array for (@arr) {print "$_\n"} # access each scalar value } __END__ $VAR1 = [ [ 0, 1, 2, 3 ], [ 10, 11, 12 ] ]; 0 1 2 3 10 11 12
    What is the best way to manipulate (add, modify, delete) a scalar value within an array?
    Adding to left (push), adding to right (unshift), delete from left (shift), delete from right (pop), delete from middle (splice). See also perlfaq4, Data: Arrays.
Re: Array of array building
by moritz (Cardinal) on Oct 01, 2008 at 16:22 UTC
    Additionally to the other references so far: perllol documents arrays of arrays, and perlreftut covers more general reference handling.
Re: Array of array building
by swampyankee (Parson) on Oct 01, 2008 at 16:25 UTC

    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

      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

Re: Array of array building
by merlyn (Sage) on Oct 01, 2008 at 17:00 UTC
Re: Array of array building
by JimEL (Initiate) on Oct 01, 2008 at 17:36 UTC
    Thanx for all the quick responses.

    Special thanx to toolic for his useful example and to merlyn for his suggestion to buy his book.

Re: Array of array building
by Anonymous Monk on Oct 01, 2008 at 16:17 UTC
    I keep getting pointers to anonymous arrays instead of the arrays or scalar values themselves.
    You're getting "references", not "pointers". See references quick reference