in reply to Multiple Levels of Array Dereferencing Have me Stumped

you wrote:
# umm -- uh.... well, the @sign implies # we have an array, but how is it # different from the first array we # dereferenced?
It's a copy!
see this example:
my $ref1 = ['a','b','c']; my $ref2 = ['d','e','f']; my $refref = [$ref1, $ref2]; myfunc($refref); print $ref1->[0]; sub myfunc { my (@list) = @{[@{$_[0]}]}; print $list[0][0]; $list[0] = ['1','2','3']; }
it prints 'aa' and not 'a1' because @{[(list of refs)]} has built a copy of 'list of refs' protecting $ref1 from being altered by the assignment inside the funcion
my 2 cents...

Replies are listed 'Best First'.
RE (tilly) 2: Multiple Levels of Array Dereferencing Have me Stumped
by tilly (Archbishop) on Nov 05, 2000 at 23:11 UTC
    Yes, but is it a useful copy? The question was about an extra reference-dereference pair that makes a copy of a copy. What do you get for that work?

    Just about nothing. There is no reason that I can see to prefer:

    sub foo { my @foo = @{[@{$_[0]}]}; # etc }
    over
    sub foo { my @foo = @{$_[0]}; # etc }
    In fact try this:
    my $bar = ["hello", "world"]; foo($bar); print "$bar->[0]\n"; sub foo { # Make my copy. my @foo = @{$_[0]}; # Try to mess up the first element. $foo[0] =~ s/h/H/; }
    See? You get a private copy already.
      I was trying to explain "what" that code does and not "why"
      however you'r right: my (@list) = @{[@{$_[0]}]}; will just build an extra (useless) copy, i didn't verified it..
        It looks to me like metaperl had already figured that out and the confusion was the belief that the extra copy had to do something useful.

        Which it doesn't.

RE: Re: Multiple Levels of Array Dereferencing Have me Stumped
by Fastolfe (Vicar) on Nov 06, 2000 at 00:41 UTC
    But wait a moment. Isn't @array = @{$arrayref} a copy also? Modifying $array[0] should have no effect upon $arrayref->[0]. So the extra level of de-referencing is puzzling.

    Setting one array to another copies the values, and by de-referencing the first time, you're building an array on the right-hand-side from which to copy.

RE: Re: Multiple Levels of Array Dereferencing Have me Stumped
by mdillon (Priest) on Nov 05, 2000 at 22:29 UTC
    i'd say the having the most correct answer is worth more than 2 cents.
(ar0n) RE (2): Multiple Levels of Array Dereferencing Have me Stumped
by ar0n (Priest) on Nov 05, 2000 at 22:49 UTC
    Occam's razor! It's great!

    [ar0n]