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

Hello All -

I am having trouble with passing an array to the second procedure below: Any Suggestions ?
#Load Array push @FruitAoA, [Fruit,Apple,Red]; push @FruitAoA, [Fruit,Bananna,Yellow]; push @FruitAoA, [Fruit,Orange,Orange]; push @FruitAoA, [Fruit,Kiwi,Green]; &PrintArray; &PrintArray2("@FruitAoA"); #Print Array with Array Name Hardcoded sub PrintArray { for $i ( 0 .. $#FruitAoA ) { for $j ( 0 .. $#{ $FruitAoA[$i] } ) { print "$FruitAoA[$i][$j]\n"; } } } #Trying to Print Array by Reference sub PrintArray2 { my $AoA = @_; for $i ( 0 .. $#AoA ) { for $j ( 0 .. $#{ $AoA[$i] } ) { print "$AoA[$i][$j]\n"; } } }
Thanks in Advance,

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

    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")

      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.
Re: Passing Array of Arrays (AoA) as a Reference?
by runrig (Abbot) on Jan 04, 2001 at 00:16 UTC
    You are not passing an array, nor an array reference, but a stringified version of the array. You should say: 'PrintArray2(\@FruitAoA)' or do what tye says below.

    You need an extra '$' in front of the AoA ('$#$AoA') since its a reference. the print can be 'print "$$AoA...' or 'print "$AoA->...' or you can (either way you should take another look through perllol:
    sub PrintArray2 { my $AoA = shift; for $aref ( @$AoA ) { for $element ( @$aref ) { print "$element\n"; } } }