disconnected copy of hash array
[email protected] (Ramprasad A Padmanabhan)
| Newsgroups | perl.beginners |
|---|---|
| Message-ID | <[email protected]> |
When I create a copy of a hash array, the operation on the copy hash
seems to affect the original hash
( when the values are reference elements )
How do I avoid this ?
In the following script %y is a copy hash , but change that and the %x
original hash is affected
--------------
#!/usr/bin/perl
use strict;
use warnings;
my %x = ( a => [1]);
foreach my $i( 1 .. 5){
my %y = %x;
push @{$y{a}},5;
print "[Loop $i] " . join(" ",@{$x{a}}) ."\n"; # I want
this to be same in every loop
}
----------------
Output
---------
[Loop 1] 1 5
[Loop 2] 1 5 5
[Loop 3] 1 5 5 5
[Loop 4] 1 5 5 5 5
[Loop 5] 1 5 5 5 5 5
-------------
But I dont want the original %x to get affected. How do I do this ?
Thanks
Ram
[Apologies , if you see this message twice. Last time my browser crashed ]