in reply to Best way to copy an array?

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

Replies are listed 'Best First'.
Re: Re: Best way to copy an array?
by John M. Dlugosz (Monsignor) on Feb 23, 2004 at 04:37 UTC
    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