Copying a hash-of-hashes

From: Peter Rabbitson (rabbit_at_rabbit.us)
Date: 12/30/04

  • Next message: Alex Daniloff: "h2xs directory structure in perl-5.8.x?"
    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


  • Next message: Alex Daniloff: "h2xs directory structure in perl-5.8.x?"

    Relevant Pages

    • Re: [PATCH 1/1] pagemap clear_refs: modify to specify anon or mapped vma clearing
      ... values a no-op similar to drop_caches at this point to avoid the potential ... adds flexibility to the user user space application implementing the reference ... That format could really be improved here, ... I am not sure what you mean by "clean" proc.txt, ...
      (Linux-Kernel)
    • Re: [PATCH 1/1] pagemap clear_refs: modify to specify anon or mapped vma clearing
      ... values a no-op similar to drop_caches at this point to avoid the potential ... adds flexibility to the user user space application implementing the reference ... That format could really be improved here, ... I am not sure what you mean by "clean" proc.txt, ...
      (Linux-Kernel)
    • Re: [PATCH 1/1] pagemap clear_refs: modify to specify anon or mapped vma clearing
      ... values a no-op similar to drop_caches at this point to avoid the potential ... adds flexibility to the user user space application implementing the reference ... That format could really be improved here, ... I am not sure what you mean by "clean" proc.txt, ...
      (Linux-Kernel)
    • Re: What is the best way to deal with a large tech doc?
      ... and am using IncludeText to reference bookmarked ... IncludeText and RD fields don't cause problems, ... Avoid hard page breaks. ... we will be creating a large technical doc ...
      (microsoft.public.word.formatting.longdocs)
    • Re: A question about efficiency
      ... if you pass a parameter by reference because you only ... want to avoid the copy, it's better to have a parameter which is a const ... Doing this, the call can be made both on a const and non-const f object, ... for an object more complex than a float the compiler may optimize the code ...
      (microsoft.public.vc.language)