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]