13

Possible Duplicate:
What's the best way to make a deep copy of a data structure in Perl?

In my code i do:

@data_new=@data;

and then I change @data.

The problem is that @data_new always changes as well. It's like @data_new is just a reference to what's in @data.

How do i make a copy of an array which is not a reference but a new copy of all the values?

@data is a two dimensional array, by the way.

Community
  • 1
  • 1
john-jones
  • 7,490
  • 18
  • 53
  • 86

2 Answers2

29

See perlfaq4's "How do I print out or copy a recursive data structure". That is, use the dclone method from Storable.

use Storable qw(dclone);
@data_new = @{ dclone(\@data) }
Emil Sit
  • 22,894
  • 7
  • 53
  • 75
20

The code you have will copy the contents of the list to a new list. However, if you are storing references in the list (and you have to in order to create a two-dimensional array in Perl), the references are copied, not the objects the references point to. So when you manipulate one of these referenced objects through one list, it appears as though the other list is changing, when in fact both lists just contain identical references.

You will have to make a "deep copy" of the list if you want to duplicate all referenced objects too. See this question for some ways to accomplish this.

Given your case of a two-dimensional array, this should work:

@data_new = map { [@$_] } @data;
Community
  • 1
  • 1
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • 9
    This isn't so much a deep copy as a "slightly less shallow" copy. :) – hobbs Dec 08 '10 at 22:40
  • 1
    @hobbs: I never claimed it was a deep copy. Read the sentence immediately preceding the code. – cdhowie Dec 09 '10 at 00:10
  • 3
    Just adding a little clarity... – hobbs Dec 09 '10 at 01:11
  • To add a bit more clarity, this answer is the two-dimensional list deep copy (2-level shallow copy). By that analogy the 1-level shallow copy is simply `@data_new = @data` and we might think of array refs as zero-level shallow. – Steven Lu Aug 22 '13 at 21:43