pelagic has asked for the wisdom of the Perl Monks concerning the following question:
Copying into @slice_1 I use map to create new anonymous data. So the problem is solved: I can change the copy without changing the original also.use strict; use Data::Dumper; my %original_hash = ( 'K1' => [ {'job' => '_DPL' ,'time' => '_MO_SU_2300' } ] ); my @slice_1 = map {{%$_}} @{$original_hash{'K1'}}; print "original_hash: ", Dumper \%original_hash; print "slice_1: ", Dumper \@slice_1; foreach my $item (@slice_1) { $item->{'job'} = 'fubar'; } print "slice_1 after manipulation: ", Dumper \@slice_1; print "original_hash after manipulation within slice_1: ", Dumper \%or +iginal_hash; my @slice_2 = @{ $original_hash{'K1'} }; foreach my $item (@slice_2) { $item->{'job'} = 'fubar'; } print "slice_2 after manipulation: ", Dumper \@slice_2; print "original_hash after manipulation within slice_2: ", Dumper \%or +iginal_hash; ___OUTPUT___ original_hash: $VAR1 = { 'K1' => [ { 'time' => '_MO_SU_2300', 'job' => '_DPL' } ] }; slice_1: $VAR1 = [ { 'time' => '_MO_SU_2300', 'job' => '_DPL' } ]; slice_1 after manipulation: $VAR1 = [ { 'time' => '_MO_SU_2300', 'job' => 'fubar' } ]; original_hash after manipulation within slice_1: $VAR1 = { 'K1' => [ { 'time' => '_MO_SU_2300', 'job' => '_DPL' } ] }; slice_2 after manipulation: $VAR1 = [ { 'time' => '_MO_SU_2300', 'job' => 'fubar' } ]; original_hash after manipulation within slice_2: $VAR1 = { 'K1' => [ { 'time' => '_MO_SU_2300', 'job' => 'fubar' } ] };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: "Physical" copy of structured data.
by japhy (Canon) on Aug 12, 2004 at 15:36 UTC | |
|
Re: "Physical" copy of structured data.
by Tomte (Priest) on Aug 12, 2004 at 15:36 UTC | |
by pelagic (Priest) on Aug 13, 2004 at 06:22 UTC |