On Fri, Mar 7, 2008 at 5:10 AM, Philipp Knechtle <[EMAIL PROTECTED]> wrote:
> Hi
>  I am working with subarrays in a main array. When I copy the main array:
>
>  my @mainarray = ([1,2,3],[4,5,6],[7,8,9])
>  my @arraytwo = @mainarray
>
>  the references get copied. When I then modify the subarray data in
>  @arraytwo they are of course also modified in @mainarray.
>
>  So I use the command:
>
>  push @arraytwo, [EMAIL PROTECTED] for @mainarray
>
>  to copy the array. Is there a nicer way to do it?

What you want is a deep copy.  The easiest way to get a deep copy is
to use dclone from Storable*:

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;
use Storable qw<dclone>;

my @mainarray = ([1,2,3],[4,5,6],[7,8,9]);
my @arraytwo  = @{dclone([EMAIL PROTECTED])};

@{$arraytwo[0]} = reverse @{$arraytwo[0]};

print Dumper([EMAIL PROTECTED], [EMAIL PROTECTED]);

* see perldoc Storable or http://perldoc.perl.org/Storable.html

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to