RE: Deep Copy
see: use strict; use Data::Dumper; sub simpleClone($){ my ($toClone) = @_; my $Return; if (ref($toClone) eq 'HASH'){ $Return = {}; while (my ($key, $value) = each(%$toClone)){ $Return-{$key} = simpleClone($value); } } elsif (ref($toClone) eq 'ARRAY') { $Return = []; @$Return = map{simpleClone($_)}(@$toClone); } else { $Return = $toClone; } return $Return; } my $a = {a=[1,2,3], b={a=4, b=5}, c=6}; my $b = simpleClone($a); print :::\n; print a: .Dumper($a); print b: .Dumper($b); print :::\n; $b-{d} = 2; print a: .Dumper($a); print b: .Dumper($b); print :::\n; $b-{c} = 2; print a: .Dumper($a); print b: .Dumper($b); print :::\n; This code is just for HASH and ARRAY and simpler SCALAR, but must be ease to be extended Thanks Marcos Rebelo -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 29, 2003 11:29 PM To: [EMAIL PROTECTED] Subject: Deep Copy I am trying to make a deep copy in a complex data structure. I have a hash that has all of its values as anonymous arrays. I need to copy the entire hash as a completely separate hash so that I can manipulate the data in the copy without affecting the original data. Copying the key of the hash is easy, that is plain data. However copying the value is harder because it is the anonymous array (so whenever I try to copy the data, it always copies the reference to the array). - my $hashref; sub hashref() { return $hashref; } $hashref-{ele1} = [val1, val2]; $hashref-{ele2} = [val3, val4]; $hashref-{ele3} = [val5, val6]; my %newhash = %$hashref; my @keys = keys(%newhash); foreach my $arraykey (@keys) { my $ref = $hashref-{$arraykey}; $newhash{$arraykey} = @$ref; } --- But then when I do: -- $newhash{ele1}-[0]=x; $newhash{ele2}-[0]=y; print $hashref-{ele1}-[0]; print $hashref-{ele2}-[0]; It changes the values in both the referenced hash and the copied hash. How do I get a real copy? TIA Brian Seel -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Deep Copy
Less code: sub simpleClone($){ my ($toClone) = @_; if (ref($toClone) eq 'SCALAR'){ my $temp = $$toClone; return \$temp; } elsif (ref($toClone) eq 'ARRAY') { return [map{simpleClone($_)}(@$toClone)]; } elsif (ref($toClone) eq 'HASH'){ return {map {$_ = simpleClone($toClone-{$_})} (keys(%$toClone))}; } elsif (not ref($toClone)) { return $toClone; } else { die Type '.ref($toClone).' not treatd in simpleClone; } } -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Wednesday, July 30, 2003 8:33 AM To: [EMAIL PROTECTED]; [EMAIL PROTECTED] Subject: RE: Deep Copy see: use strict; use Data::Dumper; sub simpleClone($){ my ($toClone) = @_; my $Return; if (ref($toClone) eq 'HASH'){ $Return = {}; while (my ($key, $value) = each(%$toClone)){ $Return-{$key} = simpleClone($value); } } elsif (ref($toClone) eq 'ARRAY') { $Return = []; @$Return = map{simpleClone($_)}(@$toClone); } else { $Return = $toClone; } return $Return; } my $a = {a=[1,2,3], b={a=4, b=5}, c=6}; my $b = simpleClone($a); print :::\n; print a: .Dumper($a); print b: .Dumper($b); print :::\n; $b-{d} = 2; print a: .Dumper($a); print b: .Dumper($b); print :::\n; $b-{c} = 2; print a: .Dumper($a); print b: .Dumper($b); print :::\n; This code is just for HASH and ARRAY and simpler SCALAR, but must be ease to be extended Thanks Marcos Rebelo -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 29, 2003 11:29 PM To: [EMAIL PROTECTED] Subject: Deep Copy I am trying to make a deep copy in a complex data structure. I have a hash that has all of its values as anonymous arrays. I need to copy the entire hash as a completely separate hash so that I can manipulate the data in the copy without affecting the original data. Copying the key of the hash is easy, that is plain data. However copying the value is harder because it is the anonymous array (so whenever I try to copy the data, it always copies the reference to the array). - my $hashref; sub hashref() { return $hashref; } $hashref-{ele1} = [val1, val2]; $hashref-{ele2} = [val3, val4]; $hashref-{ele3} = [val5, val6]; my %newhash = %$hashref; my @keys = keys(%newhash); foreach my $arraykey (@keys) { my $ref = $hashref-{$arraykey}; $newhash{$arraykey} = @$ref; } --- But then when I do: -- $newhash{ele1}-[0]=x; $newhash{ele2}-[0]=y; print $hashref-{ele1}-[0]; print $hashref-{ele2}-[0]; It changes the values in both the referenced hash and the copied hash. How do I get a real copy? TIA Brian Seel -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Deep Copy
On Wed, Jul 30, 2003 at 08:43:48AM +0200, [EMAIL PROTECTED] wrote: Less code: sub simpleClone($){ Even less[0] use Storable qw(dclone); my $dolly = dclone($dolly); Storable is also fast and comes standard with 5.8. -- Steve [0] actually quite a bit more, but you don't need to write it -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Deep Copy
There is also a good article on the topic at http://www.stonehenge.com/merlyn/UnixReview/col30.html snip Now, this simple deep_copy routine will break if there are recursive data pointers (references that point to already seen data higher in the tree). For that, you might look at the dclone method of the Storable module, found in the CPAN. /snip HTH, José. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 29, 2003 11:29 PM To: [EMAIL PROTECTED] Subject: Deep Copy I am trying to make a deep copy in a complex data structure. I have a hash that has all of its values as anonymous arrays. I need to copy the entire hash as a completely separate hash so that I can manipulate the data in the copy without affecting the original data. Copying the key of the hash is easy, that is plain data. However copying the value is harder because it is the anonymous array (so whenever I try to copy the data, it always copies the reference to the array). - my $hashref; sub hashref() { return $hashref; } $hashref-{ele1} = [val1, val2]; $hashref-{ele2} = [val3, val4]; $hashref-{ele3} = [val5, val6]; my %newhash = %$hashref; my @keys = keys(%newhash); foreach my $arraykey (@keys) { my $ref = $hashref-{$arraykey}; $newhash{$arraykey} = @$ref; } --- But then when I do: -- $newhash{ele1}-[0]=x; $newhash{ele2}-[0]=y; print $hashref-{ele1}-[0]; print $hashref-{ele2}-[0]; It changes the values in both the referenced hash and the copied hash. How do I get a real copy? TIA Brian Seel -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] DISCLAIMER This e-mail and any attachment thereto may contain information which is confidential and/or protected by intellectual property rights and are intended for the sole use of the recipient(s) named above. Any use of the information contained herein (including, but not limited to, total or partial reproduction, communication or distribution in any form) by other persons than the designated recipient(s) is prohibited. If you have received this e-mail in error, please notify the sender either by telephone or by e-mail and delete the material from any computer. Thank you for your cooperation. For further information about Proximus mobile phone services please see our website at http://www.proximus.be or refer to any Proximus agent. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Deep Copy
Thanks for all of your help! I got it with the module, but I am going to play with some of that code now :). Thanks for all of your help. Brian Seel High School Intern Micron Technology [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Deep Copy
I am trying to make a deep copy in a complex data structure. I have a hash that has all of its values as anonymous arrays. I need to copy the entire hash as a completely separate hash so that I can manipulate the data in the copy without affecting the original data. Copying the key of the hash is easy, that is plain data. However copying the value is harder because it is the anonymous array (so whenever I try to copy the data, it always copies the reference to the array). - my $hashref; sub hashref() { return $hashref; } $hashref-{ele1} = [val1, val2]; $hashref-{ele2} = [val3, val4]; $hashref-{ele3} = [val5, val6]; my %newhash = %$hashref; my @keys = keys(%newhash); foreach my $arraykey (@keys) { my $ref = $hashref-{$arraykey}; $newhash{$arraykey} = @$ref; } --- But then when I do: -- $newhash{ele1}-[0]=x; $newhash{ele2}-[0]=y; print $hashref-{ele1}-[0]; print $hashref-{ele2}-[0]; It changes the values in both the referenced hash and the copied hash. How do I get a real copy? TIA Brian Seel -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Deep Copy
Use Clone. http://search.cpan.org/author/RDF/Clone-0.13/Clone.pm use Clone qw(clone); my $newhash = clone($hashref); Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 29, 2003 5:29 PM To: [EMAIL PROTECTED] Subject: Deep Copy I am trying to make a deep copy in a complex data structure. I have a hash that has all of its values as anonymous arrays. I need to copy the entire hash as a completely separate hash so that I can manipulate the data in the copy without affecting the original data. Copying the key of the hash is easy, that is plain data. However copying the value is harder because it is the anonymous array (so whenever I try to copy the data, it always copies the reference to the array). - my $hashref; sub hashref() { return $hashref; } $hashref-{ele1} = [val1, val2]; $hashref-{ele2} = [val3, val4]; $hashref-{ele3} = [val5, val6]; my %newhash = %$hashref; my @keys = keys(%newhash); foreach my $arraykey (@keys) { my $ref = $hashref-{$arraykey}; $newhash{$arraykey} = @$ref; } --- But then when I do: -- $newhash{ele1}-[0]=x; $newhash{ele2}-[0]=y; print $hashref-{ele1}-[0]; print $hashref-{ele2}-[0]; It changes the values in both the referenced hash and the copied hash. How do I get a real copy? TIA Brian Seel -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Deep Copy
[EMAIL PROTECTED] wrote: I am trying to make a deep copy in a complex data structure. I have a hash that has all of its values as anonymous arrays. I need to copy the entire hash as a completely separate hash so that I can manipulate the data in the copy without affecting the original data. Copying the key of the hash is easy, that is plain data. However copying the value is harder because it is the anonymous array (so whenever I try to copy the data, it always copies the reference to the array). - my $hashref; sub hashref() { return $hashref; } $hashref-{ele1} = [val1, val2]; $hashref-{ele2} = [val3, val4]; $hashref-{ele3} = [val5, val6]; my %newhash = %$hashref; my @keys = keys(%newhash); foreach my $arraykey (@keys) { my $ref = $hashref-{$arraykey}; $newhash{$arraykey} = @$ref; } --- But then when I do: -- $newhash{ele1}-[0]=x; $newhash{ele2}-[0]=y; print $hashref-{ele1}-[0]; print $hashref-{ele2}-[0]; It changes the values in both the referenced hash and the copied hash. How do I get a real copy? Hi Brian. You need to recurse down the structure and copy each 'thing' according to whether it's a simple value or a hash or array reference. The code below will work, but it's by no means foolproof - there can be references to a lot of things other than hashes or arrays! HTH, Rob use strict; use warnings; my $hashref; $hashref-{ele1} = ['val1', 'val2']; $hashref-{ele2} = ['val3', 'val4']; $hashref-{ele3} = ['val5', 'val6']; my $newhash = deep_copy($hashref); $newhash-{ele1}[0] = 'x'; $newhash-{ele2}[0] = 'y'; print $hashref-{ele1}[0], \n; print $hashref-{ele2}[0], \n; print $newhash-{ele1}[0], \n; print $newhash-{ele2}[0], \n; sub deep_copy { my $val = shift; my $ref = ref $val; my $copy; if ($ref eq 'HASH') { my ($k, $v); $copy-{$k} = deep_copy($v) while ($k, $v) = each %$val; } elsif ($ref eq 'ARRAY') { my $i = 0; $copy-[$i++] = deep_copy($_) foreach @$val; } else { $copy = $val; } return $copy; } OUTPUT val1 val3 x y -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Deep copy
Hey anyone have the link handy that explained deep copying and had the simplest little code snip to make deep copies? Nikola Janceski We are such stuff as dreams are made on, rounded with a little sleep. -- William Shakespeare The views and opinions expressed in this email message are the sender's own, and do not necessarily represent the views and opinions of Summit Systems Inc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Deep copy
What you mean by deep copy ? Be more clear :-) José. -Original Message- From: Nikola Janceski [mailto:[EMAIL PROTECTED]] Sent: Friday, August 09, 2002 3:40 PM To: Beginners (E-mail) Subject: Deep copy Hey anyone have the link handy that explained deep copying and had the simplest little code snip to make deep copies? Nikola Janceski We are such stuff as dreams are made on, rounded with a little sleep. -- William Shakespeare The views and opinions expressed in this email message are the sender's own, and do not necessarily represent the views and opinions of Summit Systems Inc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] DISCLAIMER This e-mail and any attachment thereto may contain information which is confidential and/or protected by intellectual property rights and are intended for the sole use of the recipient(s) named above. Any use of the information contained herein (including, but not limited to, total or partial reproduction, communication or distribution in any form) by other persons than the designated recipient(s) is prohibited. If you have received this e-mail in error, please notify the sender either by telephone or by e-mail and delete the material from any computer. Thank you for your cooperation. For further information about Proximus mobile phone services please see our website at http://www.proximus.be or refer to any Proximus agent. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Deep copy
http://www.stonehenge.com/merlyn/UnixReview/col30.html Found it. Interesting read once you get into large complex data structures. -Original Message- From: Nikola Janceski [mailto:[EMAIL PROTECTED]] Sent: Friday, August 09, 2002 9:46 AM To: 'NYIMI Jose (BMB)'; Nikola Janceski; Beginners (E-mail) Subject: RE: Deep copy Deep copy. I have a data structure (hashes of hashes) I want to make a real/deep copy of the values to store elsewhere. So when I change the values of one, the references don't point to the same values as the original data structure. hence deep copy. -Original Message- From: NYIMI Jose (BMB) [mailto:[EMAIL PROTECTED]] Sent: Friday, August 09, 2002 9:44 AM To: Nikola Janceski; Beginners (E-mail) Subject: RE: Deep copy What you mean by deep copy ? Be more clear :-) José. -Original Message- From: Nikola Janceski [mailto:[EMAIL PROTECTED]] Sent: Friday, August 09, 2002 3:40 PM To: Beginners (E-mail) Subject: Deep copy Hey anyone have the link handy that explained deep copying and had the simplest little code snip to make deep copies? Nikola Janceski We are such stuff as dreams are made on, rounded with a little sleep. -- William Shakespeare -- -- The views and opinions expressed in this email message are the sender's own, and do not necessarily represent the views and opinions of Summit Systems Inc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] DISCLAIMER This e-mail and any attachment thereto may contain information which is confidential and/or protected by intellectual property rights and are intended for the sole use of the recipient(s) named above. Any use of the information contained herein (including, but not limited to, total or partial reproduction, communication or distribution in any form) by other persons than the designated recipient(s) is prohibited. If you have received this e-mail in error, please notify the sender either by telephone or by e-mail and delete the material from any computer. Thank you for your cooperation. For further information about Proximus mobile phone services please see our website at http://www.proximus.be or refer to any Proximus agent. -- -- The views and opinions expressed in this email message are the sender's own, and do not necessarily represent the views and opinions of Summit Systems Inc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] The views and opinions expressed in this email message are the sender's own, and do not necessarily represent the views and opinions of Summit Systems Inc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]