Click here to get back home

disjoint copy one hash to another

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
disjoint copy one hash to another ram@pragatee.com 05-27-2008
Get Chitika Premium
Posted by ram@pragatee.com on May 27, 2008, 6:04 am
Please log in for more thread options
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 @},5;
print "[Loop $i] " . join(" ",@}) ."\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
ram@pragatee.com
http://pragatee.com







Posted by Jens Thoms Toerring on May 27, 2008, 6:27 am
Please log in for more thread options
> 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 )

> 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]);

So $x is an array reference, not an array. So $x is
a pointer to an array that's stored somewhere in memory.

> foreach my $i( 1 .. 5){
> my %y = %x;

Here this copy copies the reference, so $x and $y now
point to the same array. The copy operation isn't a "deep
copy" that instead of copying the reference copies what it
points to. A "deep copy" would result in a comparison like

$x == $y

to fail (unless you also change the meaning of the '=='
operator etc. if applied to references) which could get a
bit surprising...

> push @},5;

And here you change the array that both $x and $y point
to.

> How do I avoid this ?

To get around that you will have to create a new array which
$y pointis to and copy the elements of the array $x
points to. So use not just

my %y = %x;

but add

$y = [ @} ];

before you start to change $y.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de

Posted by A. Sinan Unur on May 27, 2008, 6:33 am
Please log in for more thread options
47bc-80e9-af618e3dece4@i36g2000prf.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 ?

See dclone in Storable:

http://search.cpan.org/~ams/Storable-2.18/Storable.pm

Sinan

--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Posted by Ben Morrow on May 27, 2008, 6:47 am
Please log in for more thread options

> 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 ?

use Clone; # or one of the Clone::* alternatives

Ben

--
"If a book is worth reading when you are six, * ben@morrow.me.uk
it is worth reading when you are sixty." [C.S.Lewis]

Similar ThreadsPosted
Cloning classes, deep copy revisited (Was: Copy Consturctor in Perl) .. November 23, 2008, 3:33 pm
Use of CGI.pm filehandle with File::Copy::copy in taint mode May 5, 2006, 3:57 pm
"copy" from File::Copy fails but returns success October 16, 2007, 4:43 am
Copy all January 21, 2007, 11:53 am
copy help. May 16, 2007, 1:33 am
file copy November 19, 2004, 2:45 pm
FAQ 5.5 How can I copy a file? May 2, 2005, 5:03 pm
copy contructor July 15, 2005, 12:53 pm
FAQ 5.5 How can I copy a file? July 18, 2005, 4:03 pm
FAQ 5.5 How can I copy a file? September 22, 2005, 4:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap