Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Best way to copy an array?

by John M. Dlugosz (Monsignor)
on Feb 22, 2004 at 06:18 UTC ( #330894=perlquestion: print w/ replies, xml ) Need Help??
John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

I need to copy an array, so that assignments made to the copy don't affect the original, but the elements reference the same things initially.

@newlist= map {$_} (@{$self->{oldlist}});
seems to be the right idea, but is going a bit out of the way. Is there a nicer way to do it?

—John

Comment on Best way to copy an array?
Download Code
Re: Best way to copy an array?
by jweed (Chaplain) on Feb 22, 2004 at 06:22 UTC
    For non-huge arrays, this should work fine:
    @newlist = @{$self->{oldlist}};
    This will copy the elements but not alias them: i.e., it will make 2 separate arrays that happen to start out with the same elements.



    Code is (almost) always untested.
    http://www.justicepoetic.net/
Re: Best way to copy an array?
by blokhead (Monsignor) on Feb 22, 2004 at 06:56 UTC
    assignments made to the copy don't affect the original, but the elements reference the same things initially.
    Am I missing something? What exactly is wrong with @newlist = @{$self->{oldlist}};? What do you mean by "reference the same things initially?" Is this an array of references? Maybe you mean something like
    @newlist = map { [ @$_ ] } @{ $self->{oldlist} }; @newlist = map { { %$_ } } @{ $self->{oldlist} };
    but it's hard to tell..

    blokhead

      Am I missing something? What exactly is wrong with @newlist = @{$self->{oldlist}};?

      I thought that doesn't copy anything, but aliases the array. You mean I'm copying a list every time I dereference it?!

      No, 'cause push @{$self->{oldlist}}, $x does indeed modify the thing being remembered by self.

      Ah, assigning to a @list naturally copies the elements and keeps the identity of the left-hand-side. That makes sence.

      Thanks,
      —John

Re: Best way to copy an array?
by NetWallah (Abbot) on Feb 22, 2004 at 07:03 UTC
    @newlist= @{$self->{oldlist}}; # Direct copy #Alternative ... push @newlist, @{$self->{oldlist}}; # Stuff it in

    "Experience is a wonderful thing. It enables you to recognize a mistake when you make it again."
•Re: Best way to copy an array?
by merlyn (Sage) on Feb 22, 2004 at 12:07 UTC
Re: Best way to copy an array?
by BrowserUk (Pope) on Feb 22, 2004 at 12:27 UTC

    If there is any chance that the elements of your array are themselves compound (ie. array or hashes), then you may like to look at Clone. It's much quicker than any pure perl method, and it doesn't get confused by deeply nested structures.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Timing (and a little luck) are everything!
Re: Best way to copy an array?
by flyingmoose (Priest) on Feb 22, 2004 at 23:50 UTC

    Oooh! Oooh! I know this one...

    memcpy()

    Oh wait, wrong language. Darn.

    (edit: not like I care, but easy with the modpoints guys -- it's a joke! -- memcpy is one of my favorite C commands -- very useful, and I abuse it often.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2014-07-26 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls