in reply to Unwanted cloning of array elements

$piece is a reference. You push the same reference onto your arrayref 3 times. It does not matter when or how you change some sub-part of the thing the reference points to. If you want 3 different elements in your arrayref then you are going to have to deep-copy $piece at every push.


🦛

Replies are listed 'Best First'.
Re^2: Unwanted cloning of array elements
by oakbox (Chaplain) on May 07, 2021 at 10:37 UTC

    Hippo! Thank you so much for that link on deep-copy mechanics. That's exactly what I needed :)

    Fixed code

    #!/usr/bin/perl use strict; my $Action; my $piece; $piece->{this} = 123; $piece->{that} = 'some text'; foreach my $other (111...113){ my $thispiece = {%$piece}; # create a deep copy $thispiece->{theother} = $other; push(@{$Action}, $thispiece); } use Data::Dumper; print Dumper($Action);

    And now the output looks like I expected it to:

    $VAR1 = [ { 'that' => 'some text', 'theother' => 111, 'this' => 123 }, { 'theother' => 112, 'that' => 'some text', 'this' => 123 }, { 'this' => 123, 'theother' => 113, 'that' => 'some text' } ];

    Hippo FTW!

      my $thispiece = {%$piece};  # create a deep copy

      Be aware that {%$piece} creates a shallow copy. It may appear to be deep, but only because $piece is shallow to begin with, i.e., it has only one level. Try your code with a nested structure for $piece:

      Win8 Strawberry 5.8.9.5 (32) Fri 05/07/2021 7:11:33 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings my $Action; my $piece = { this => { that => { foo => { bar => 123 } } } }; foreach my $other (111 .. 113){ my $thispiece = {%$piece}; # create a SHALLOW copy $thispiece->{theother} = $other; push(@{$Action}, $thispiece); } use Data::Dumper; print Dumper($Action); ^Z $VAR1 = [ { 'theother' => 111, 'this' => { 'that' => { 'foo' => { 'bar' => 123 } } } }, { 'theother' => 112, 'this' => $VAR1->[0]{'this'} }, { 'theother' => 113, 'this' => $VAR1->[0]{'this'} } ];
      Of course, if you're only dealing with a shallow structure in $piece to begin with, your code will be fine.


      Give a man a fish:  <%-{-{-{-<