Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Questions about using array-of-arrays

by jep1978 (Novice)
on Jul 26, 2007 at 11:54 UTC ( #628899=perlquestion: print w/replies, xml ) Need Help??

jep1978 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm a fairly basic Perl user trying to troubleshoot a script that involves arrays-of-arrays. I think something is going wrong in the way I move information to and from the arrays-of-arrays, but I can't figure out what.

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] };

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 ];

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] };

Thanks to anyone who can help lead me out of this bracket-maze!

Replies are listed 'Best First'.
Re: Questions about using array-of-arrays
by FunkyMonk (Chancellor) on Jul 26, 2007 at 12:05 UTC
    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

Re: Questions about using array-of-arrays
by moritz (Cardinal) on Jul 26, 2007 at 12:08 UTC
    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 :()

Re: Questions about using array-of-arrays
by clinton (Priest) on Jul 26, 2007 at 12:07 UTC
    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

      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.
Re: Questions about using array-of-arrays
by Grey Fox (Chaplain) on Jul 26, 2007 at 14:17 UTC
    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
Re: Questions about using array-of-arrays
by fmerges (Chaplain) on Jul 26, 2007 at 12:12 UTC

    Hi,

    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
Re: Questions about using array-of-arrays
by varchana (Initiate) on Jul 27, 2007 at 08:51 UTC
    I will suggest you too use for loop

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://628899]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2022-09-26 09:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (117 votes). Check out past polls.

    Notices?