disjoint copy one hash to another

[email protected] (Ram Prasad)
Newsgroups perl.beginners
Organization http://groups.google.com
Message-ID <fdf422d8-94d9-4146-b333-4187cb6980a5@y22g2000prd.googlegroups.com>
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















PS:
Note to Spammers: Go ahead , send me spam
[email protected]
http://pragatee.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.