Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Questions about using array-of-arrays

by FunkyMonk (Chancellor)
on Jul 26, 2007 at 12:05 UTC ( [id://628902]=note: print w/replies, xml ) Need Help??


in reply to Questions about using array-of-arrays

If I want to take one "row" of my array-of-arrays and copy it to a separate array, is this correct: @SeparateArray = @{ MyAoA[$i] };
Barring the typo(?), yes that's right:
@SeparateArray = @{ $MyAoA[$i] };

And if I want to do the opposite (copy my separate array into a row of my array-of-arrays), is this correct: @{ $MyAoA[$i] } = [ @SeparateArray ];
No, the types of either side of = must be, erm, equal. The first example below will add a reference to @SeparateArray to your AoA. That is, if you later change @SeparateArray, it'll change the contents of your AoA. Use the second example below if you don't want that to happen.

$MyAoA[$i] = \@SeparateArray; # reference @{ $MyAoA[$i] } = @SeparateArray; # copy

Finally, if I want to copy an entire array-of-arrays (and not just have references to the first one), can I simply write: @NewAoA = [ @OldAoA ]; or do I need to build a "for" loop to replace each component array separately, e.g.: @{ $NewAoA[$i] } = @{ $OldAoA[$i] };
Yes, you need a for loop to make copies of each of the array references.

Have you seen the Data Structures Cookbook?

update: fixed some typos
update^2: Added link to perldsc

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found