Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Creating a copy of an array

by nsluis (Initiate)
on Dec 19, 2000 at 03:13 UTC ( #47285=perlquestion: print w/replies, xml ) Need Help??

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

Hi! I was playing around with arrays and came across something I didn't expect: @first = ( '1','2','3', '11','22','33' ); @second = @first; $first[0]2 = 4; print "$second[0]2\n"; The result will be 4. I don't want that :) How can I create a copy of the first array without them being 'linked'? TIA, Nanne

Replies are listed 'Best First'.
Re: Creating a copy of an array
by chipmunk (Parson) on Dec 19, 2000 at 03:26 UTC
    How do I print out or copy a recursive data structure?, from perlfaq4, answers this question. However, the docs on this site are a little out of date; this particular FAQ has been updated in newer versions. Use `perldoc -q copy` to look up the answer locally, if you have a more recent version of perl than 5.005_02, or check the docs at http://www.perl.com.

    As to why you had this problem... @second = @first only does a shallow copy. The references in @first are copied to @second, so that @{$second[0]} is the exact same array as @{$first[0]}.

Re: Creating a copy of an array
by merlyn (Sage) on Dec 19, 2000 at 04:41 UTC
Re:Creating a copy of an array
by I0 (Priest) on Dec 19, 2000 at 04:39 UTC
    @second = map{[@_]} @first; #or more generally use Storable(dclone); @second = @{dclone(\@first)};
Re: Creating a copy of an array
by ichimunki (Priest) on Dec 19, 2000 at 04:38 UTC
    my @first = ( ['1','2','3'], ['11','22','33'] ); my @second = map { reref ( @$_ ) } @first; $first[0][2] = 4; print "$first[0][2]\n"; print "$second[0][2]\n"; sub reref { my @out = (); push (@out, $_) for @_; return \@out; }

    This builds a new anonymous list reference for each list in the list. There is probably a better solution, but this works for your stated problem.

    For the record, I have decided to purge the word array from my Perl vocabulary. Perl's lists have only the slightest relationship to arrays in other languages.
      How about: my @second = map { [ @$_ ] ] @first; That expands each element then creates a new array ref from it. Without the slowdown of a subroutine call like you used.

      Still there are modules that can do this better.

      Hell you could do this (not recommended, there are modules made for copying).

      use Data::Dumper qw/Dumper/; my @first = ( ['1','2','3'], ['11','22','33'] ); my @second = @{ eval Dumper(\@first) };
        Cool. I think using turning the sub into an expression is much better. So how does any module beat a one liner? :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2022-06-26 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (83 votes). Check out past polls.

    Notices?