in reply to Passing Array of Arrays (AoA) as a Reference?

In &PrintArray2("@FruitAoA"); you need to drop the quotes.

In my $AoA = @_; this fetches the number of arguments passed in. You probably meant my @AoA= @_;.

Update: OK, I even tested your code and with those two fixes it works. I strongly suggest that from now on you start each of your scripts with:

#!/usr/bin/perl -w use strict;
which would force you to change some things to be more like:
my @FruitAoA; push @FruitAoA, [qw(Fruit Apple Red)]; push @FruitAoA, [qw(Fruit Bananna Yellow)]; push @FruitAoA, [qw(Fruit Orange Orange)]; push @FruitAoA, [qw(Fruit Kiwi Green)];
or just
my @FruitAoA= [qw(Fruit Apple Red)], [qw(Fruit Bananna Yellow)], [qw(Fruit Orange Orange)], [qw(Fruit Kiwi Green)], );
and
for my $i ( 0 .. $#FruitAoA )
Doing this will make it much easier for you to spot similar mistakes in the future.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: Passing Array of Arrays (AoA) as a Reference?
by davorg (Chancellor) on Jan 04, 2001 at 00:25 UTC

    Reading between the lines, I think he meant

    ($AoA) = @_;

    but meant to call the function using a reference

    &PrintArray2(\@FruitArray);

    Once more I point out that using use strict and -w would have caught this error.

    Update: tye's fixes do, of course, work. But I think that the intention was that the second example would involve passing the array to the sub as reference. In that case, the second sub is very broken. Here's a fixed version (which also passes -w and use strict).

    #!/usr/local/bin/perl -w use strict; #Load Array my @FruitAoA = ([qw/Fruit Apple Red/], [qw/Fruit Bananna Yellow/], [qw/Fruit Orange Orange/], [qw/Fruit Kiwi Green/]); &PrintArray; &PrintArray2(\@FruitAoA); #Print Array with Array Name Hardcoded sub PrintArray { for my $i ( 0 .. $#FruitAoA ) { for my $j ( 0 .. $#{ $FruitAoA[$i] } ) { print "$FruitAoA[$i][$j]\n"; } } } #Trying to Print Array by Reference sub PrintArray2 { my ($AoA) = @_; for my $i ( 0 .. $#{$AoA} ) { for my $j ( 0 .. $#{ $AoA->[$i] } ) { print "$AoA->[$i][$j]\n"; } } }
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      Thanks for all your help on this one !! I had a 764 line script that can now be shrunk to 160 lines now that the arrays can be passed instead of hardcoded.