Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: (tye)Re: Passing Array of Arrays (AoA) as a Reference?

by davorg (Chancellor)
on Jan 04, 2001 at 00:25 UTC ( [id://49615]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://49615]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-23 09:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found