The stupid question is the question not asked | |
PerlMonks |
Re: Questions about using array-of-arraysby FunkyMonk (Chancellor) |
on Jul 26, 2007 at 12:05 UTC ( [id://628902]=note: print w/replies, xml ) | Need Help?? |
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:
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.
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
In Section
Seekers of Perl Wisdom
|
|