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

Dear wise monks,

I have tried and tried and have failed to construct
an Array of Array (AoA) and access an element in that
AoA.

Here is the new method of my object ...
sub new { my ( $pkg, $cols, $rows, $firstRow ) = @_; # where first Row is a ref to an array. my @AoA = (); # deref $firstRow and push the first array # into the array or arrays push ( @AoA, @$firstRow ); for( my $row=1; $row<$rows; $row++ ) { push( @AoA, [0..$cols] ); # I bet this wrong } bless { cols => $cols, rows => $rows, AoA => @AoA }, $pkg; }
... The above new method does not cause a "compile time" error
but I suspect that it might be why this method does not work.
sub printElement { $obj = shift; $row = shift; $col = shift; print "AoA[$row][$col] = $obj->{AoA}[$row][$col]; # line 42 }
... The error I keey running into is ...
Can't use string ("blah") as array ref while "strict refs" in use at My.pm line 42.

What am I doing wrong here?
Thanks!

Edit kudra, 2002-05-05 Added what might have been missing from the title

Replies are listed 'Best First'.
Re: How does one
by thelenm (Vicar) on May 03, 2002 at 21:37 UTC
    I'm assuming that $rows and $cols contain the dimensions of the two-dimensional array you want to end up with. And are you trying to initialize it so that you do the following for all values of $row and $col?
    $AoA[$row][$col] = $col;
    That's basically what you're doing, except for this line:
    push(@AoA, @$firstRow);
    This code causes @AoA itself to be a copy of the array referenced by $firstRow. Since an array of arrays needs to be an array of array references, this is not what you want. Get rid of that line, or possibly change it to:
    push(@AoA, $firstRow);

    Also, in your bless statement, you need to change @AoA to \@AoA. Again, nested data structures need to use references. Try making those changes, and see if it works more as you expect.

    Oh yeah, and you may also find these man pages helpful: perlref, perldsc, perllol.

Re: How does one
by JayBonci (Curate) on May 03, 2002 at 21:42 UTC
    Okay, removing all of your class reference stuff, it looks like firstRow is not an array reference. Add this to your code, right after your first line in the new statement.
    die "Ack! That's no reference. IT'S A MAN BABY\n" unless(ref $firstR +ow eq "ARRAY);
    That will let you know if you're actually getting a reference to an array. Watered down, without the object stuff, this code works for me:
    use strict; my @AoA; push @AoA, [1..40] for(1..20); print ref $AoA[0]."\n"; print "".(ref $AoA[0])."\n"; print $AoA[0][1]."\n"; Producing (correctly): ARRAY 2
    This looks different than your code in a couple of places. First off (and apologies for the offtopic-ness, but it may simplify your code a bit), since you never use $row, you can simply your for statement to be:
    for(1..$rows)
    Ahh, a fellow C programmer ;). This is a range, and will be interpreted correctly by the for statement. Pushing the 1..40 or (1..$rows) in array context works fine for making 2d arrays.

    Your best buddy here is ref. It will tell you whether you can make something into an array or not. For instance... if you
    print ref $obj->{AoA};
    ...it will tell you whether you can use it as an ARRAY. Now if you
    print ref $obj->{AoA}[$row];
    ...that will tell you that it's either a SCALAR or (more likely) not a reference (a blank string) in your previous trouble. This means that it can't be dereferenced as an array.

    Hope that it's enough to get you started!

        --jb
(crazyinsomniac: DumperX) Re: How does one
by crazyinsomniac (Prior) on May 04, 2002 at 01:23 UTC
Re: How does one
by tachyon (Chancellor) on May 04, 2002 at 07:32 UTC

    push array refs (not arrays) into your array, vis:

    use Data::Dumper; $ary1 = [ qw( foo bar baz ) ]; $ary2 = [ qw( just another perl hacker ) ]; push @AoA, $ary1, $ary2; print $AoA[1][3], " ", $AoA[0][0], "!\n"; print Dumper \@AoA;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

A reply falls below the community's threshold of quality. You may see it by logging in.