I want to back up the hash, keep the original hash, and use the backed up hash data.
And deleted the backup hash data.
However, the original hash data has been deleted.
Here is the code.
my %hash = (
'data1' => {
'data2' => {
'data3' => 'one',
},
},
);
foreach (1..3) {
my %hash_backup = %hash;
print $hash{'data1'}->{'data2'}->{'data3'},"\n";
print $hash_backup{'data1'}->{'data2'}->{'data3'},"\n";
print "-------------------------------\n";
delete $hash_backup{'data1'}->{'data2'};
print $hash{'data1'}->{'data2'}->{'data3'},"\n";
print $hash_backup{'data1'}->{'data2'}->{'data3'},"\n";
print "================================\n";
}
Result,
one
one
-------------------------------
================================
-------------------------------
================================
-------------------------------
================================
If you change the delete code, it works normally.
delete $hash_backup{'data1'};
Result,
one
one
-------------------------------
one
================================
one
one
-------------------------------
one
================================
one
one
-------------------------------
one
================================
I think it is a hash reference problem.
How can I keep the original hash and delete the backup hash?