in reply to (tye)Re: Passing Array of Arrays (AoA) as a Reference?
in thread Passing Array of Arrays (AoA) as a Reference?

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

Replies are listed 'Best First'.
Re: Re: (tye)Re: Passing Array of Arrays (AoA) as a Reference?
by elpienck1 (Initiate) on Jan 04, 2001 at 03:47 UTC
    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.