Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: How do i create array of arrays

by Adam (Vicar)
on Jan 13, 2001 at 07:08 UTC ( [id://51547]=note: print w/replies, xml ) Need Help??


in reply to How do i create array of arrays

my @array = ( 1..3 ); my $arrayRef = \@array; my @arrayOfArrays = ($arrayRef); print $arrayOfArrays[0]->[1];
I hope that helps.

{Editor note by davido: This approach is risky. For one thing, if @array ever changes, its changes will affect @arrayOfArrays. Thus, if you build up an array of arrays using this method inside of a loop, you could end up with multiple instances of the same @array, rather than multiple distinct and unique arrays as the contents of the @arrayOfArrays. Using my within the loop will create a new @array each time through, but better to be explicit about it rather than relying on lexical scoping which can confuse the reader later on.

Changing the second line to:

my @arrayRef = [@array];
will solve the potential problem.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-19 06:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found