R
rosenthd
Hi,
I'd like to use a copy of my "test" object below without it being
changed. However, all of my attempts so far to copy it don't seem
to succeed. As the example shows below changing newarray affects the
test object. How Can I copy values from the test array into a new
array without changing the original test array.
$test[0]{'A'} = 1;
$test[1]{'A'} = 1;
$test[2]{'A'} = 2;
$test[2]{'C'} = 3;
$test[3]{'B'} = 3;
@newarray = @test;
$testref = \@test;
for ($x=0;$x<=$#{$testref};$x++)
{
#print "$x\n";
foreach $elem (keys %{${$testref}[$x]})
{
print "$x: $elem, ${$testref}[$x]{$elem}\n";
}
}
print "Setting G to 3\n";
$newarray[0]{'G'}=3;
for ($x=0;$x<=$#{$testref};$x++)
{
#print "$x\n";
foreach $elem (keys %{${$testref}[$x]})
{
print "$x: $elem, ${$testref}[$x]{$elem}\n";
}
}
Here's my output:
0: A, 1
1: A, 1
2: A, 2
2: C, 3
3: B, 3
Setting G to 3
0: A, 1
0: G, 3
1: A, 1
2: A, 2
2: C, 3
3: B, 3
I'd like to use a copy of my "test" object below without it being
changed. However, all of my attempts so far to copy it don't seem
to succeed. As the example shows below changing newarray affects the
test object. How Can I copy values from the test array into a new
array without changing the original test array.
$test[0]{'A'} = 1;
$test[1]{'A'} = 1;
$test[2]{'A'} = 2;
$test[2]{'C'} = 3;
$test[3]{'B'} = 3;
@newarray = @test;
$testref = \@test;
for ($x=0;$x<=$#{$testref};$x++)
{
#print "$x\n";
foreach $elem (keys %{${$testref}[$x]})
{
print "$x: $elem, ${$testref}[$x]{$elem}\n";
}
}
print "Setting G to 3\n";
$newarray[0]{'G'}=3;
for ($x=0;$x<=$#{$testref};$x++)
{
#print "$x\n";
foreach $elem (keys %{${$testref}[$x]})
{
print "$x: $elem, ${$testref}[$x]{$elem}\n";
}
}
Here's my output:
0: A, 1
1: A, 1
2: A, 2
2: C, 3
3: B, 3
Setting G to 3
0: A, 1
0: G, 3
1: A, 1
2: A, 2
2: C, 3
3: B, 3