Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Unwanted cloning of array elements

by oakbox (Chaplain)
on May 07, 2021 at 10:37 UTC ( [id://11132207]=note: print w/replies, xml ) Need Help??


in reply to Re: Unwanted cloning of array elements
in thread Unwanted cloning of array elements

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!

Replies are listed 'Best First'.
Re^3: Unwanted cloning of array elements
by AnomalousMonk (Archbishop) on May 07, 2021 at 11:19 UTC
    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:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 19:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found