copy subarray - Perl
This is a discussion on copy subarray - Perl ; 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 ...
-
copy subarray
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, [@{$_}] for @mainarray
to copy the array. Is there a nicer way to do it?
-
Re: copy subarray
On Fri, Mar 7, 2008 at 5:10 AM, Philipp Knechtle <philippk@evolva.com> 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, [@{$_}] 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:
umper;
use Storable qw<dclone>;
my @mainarray = ([1,2,3],[4,5,6],[7,8,9]);
my @arraytwo = @{dclone(\@mainarray)};
@{$arraytwo[0]} = reverse @{$arraytwo[0]};
print Dumper(\@mainarray, \@arraytwo);
* 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.
-
Re: copy subarray
In python or C++ there is a concept called "deep copy".
try to search that keyword on cpan, like this one:
http://search.cpan.org/~stevan/Class...s/Cloneable.pm
On Fri, Mar 7, 2008 at 6:10 PM, Philipp Knechtle <philippk@evolva.com> 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, [@{$_}] for @mainarray
>
>
>
> to copy the array. Is there a nicer way to do it?
>
>
>
>
-
Re: copy subarray
From: "Philipp Knechtle" <philippk@evolva.com>
To: <beginners@perl.org>
> 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, [@{$_}] for @mainarray
>
> to copy the array. Is there a nicer way to do it?
http://search.cpan.org/search?query=Clone&mode=module
Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
-
Re: copy subarray
Philipp Knechtle wrote:
> Hi
Hello,
> 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, [@{$_}] for @mainarray
>
> to copy the array. Is there a nicer way to do it?
For your simple AoA example:
my @arraytwo = map [@$_], @mainarray;
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall