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
| [reply] [d/l] [select] |
You can use Storable::dclone to copy Arrays of Arrays (and other data structures):
use Storable qw(dclone);
my @a = ([2, 3], [5, 6]);
my $copy_ref = dclone(\@a);
my @copy = @{ dclone(\@a) };
(Untested, no perl available atm :()
| [reply] [d/l] |
take one "row" of my array-of-arrays and copy it to a separate array:
@SeparateArray = @{ MyAoA[$i] };
@SeparateArray = @{ $MyAoA[$i] };
copy my separate array into a row of my array-of-arrays
@{ $MyAoA[$i] } = [ @SeparateArray ];
$MyAoA[$i] = \@SeparateArray ;
copy an entire array-of-arrays (and not just have references to the first one)
push @new_array,[@$_] foreach @old_array
or
@new_array = map {[@$_]} @old_array
(note, this last one only works for AoA, not for AoAoA and deeper. For that situation, it may be easier to use Storable's dclone method)
Clint
| [reply] [d/l] [select] |
copy my separate array into a row of my array-of-arrays
I'd point explicitly out that there is a difference between taking a reference and copying the data within the array:
# Here, changes in @SeparateArray will be "seen" in
# $MyAoA[$i] too
$MyAoA[$i] = \@SeparateArray;
pop @SeparateArray; # $MyAoA[$i] affected, too
# Here, $MyAoA[$i] and @SeparateArray are somewhat
# independent, even if they may share common elements
$MyAoA[$i] = [ @SeparateArray ];
pop @SeparateArray; # no change to $MyAoA[$i]
This is something that remains somewhat "implicit" in your answer, e.g. when you talk about how to copy an entire array-of-arrays.
Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
Don't fool yourself.
| [reply] [d/l] |
Hi Jep
One of the things that has helped me figure out where I am in an array is data dumper. When you can see the array physically layed out it really helps.
#!/pw/prod/svr4/bin/perl
use warnings;
use strict;
use Data::Dumper;
my $array;
print Dumper(@array);
Hope this helps
-- Grey Fox
"We are grey. We stand between the darkness and the light" B5
| [reply] [d/l] |
my @SeparateArray = @{ $MyAoA[$i] };
$MyAoA[$i] = \@SeparateArray;
require Storable;
my @Copy = @{ Storable::dclone(\@MyAoA) };
Update: BTW Read the perlref manpage.
Regards,
fmerges at irc.freenode.net
| [reply] [d/l] |
I will suggest you too use for loop | [reply] |