Copying a hash-of-hashes
From: Peter Rabbitson (rabbit_at_rabbit.us)
Date: 12/30/04
- Previous message: Bob Showalter: "RE: Trouble running a script with cron"
- Next in thread: Jose Nyimi: "RE : Copying a hash-of-hashes"
- Reply: Jose Nyimi: "RE : Copying a hash-of-hashes"
- Reply: Dave Gray: "Re: Copying a hash-of-hashes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 29 Dec 2004 22:58:17 -0500 To: beginners@perl.org
Hello List,
To explain the problem I am having, I wrote a simple snipet that doesn't do
anything meaningful other than illustrating the behaviour I want to avoid:
my %hoh = (
1 => {
a => 5, b => 5
},
2 => 5
);
my @res = &RANDOMSUB; #call a random subroutine
print "$hoh{1}{a} $hoh{2} \n";
sub RANDOMSUB {
my %hohcopy = %hoh;
$hohcopy{1}{a} = 2;
$hohcopy{2} = 2;
}
>From my understanding since in the subroutine I am working with a local hash
%hohcopy all the values assigned to it have no meaning in the main body.
However the print produces 2, 5 instead of the desired 5, 5. As far as my
understanding goes this is due to the fact that $hohcopy{1} holds a
reference to $hoh{1} instead of recreating it's internal structure entirely
from the root to the leaves while performing a copy.
The question is whether there is an elegant way to produce a complete copy
of a hash-of-hashes-of-hashes-...-of-hashes for internal subroutine purposes
and make sure that all references will be translated properly as well,
leaving the subroutine no ability to modify the main hash.
Thank you.
Peter
- Previous message: Bob Showalter: "RE: Trouble running a script with cron"
- Next in thread: Jose Nyimi: "RE : Copying a hash-of-hashes"
- Reply: Jose Nyimi: "RE : Copying a hash-of-hashes"
- Reply: Dave Gray: "Re: Copying a hash-of-hashes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|