Reference to hash value

Discussion in 'Perl Misc' started by schwarzenschafe@gmail.com, Sep 26, 2006.

  1. Guest

    What's a better way to write the following:

    %hash = %{$hashref1->{"key"}};
    $hashref2 = \%hash;

    Assuming $hashref1->{"key"} is a hashref I want to copy (not
    reference), and %hash is never used. My best guess is:

    $hashref2 = \%{$hashref1->{"key"}};

    But that references $hashref1->{"key"} rather than copies it (ie doesnt
    work). Google and the docs didn't help me. Thanka,

    SS
    , Sep 26, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > Assuming $hashref1->{"key"} is a hashref I want to copy (not
    > reference), and %hash is never used.


    I'm not sure I understand your question, but I THINK you are asking how
    you can copy an entire hash structure (ie, "deep" copy), and not just a
    copy of the reference itself (ie, "shallow copy"). If this is your
    question, you may be interested in the helpful replies I received to a
    similar inquiry; see http://tinyurl.com/fcj8p (and you will probably
    want to learn about the dclone method of the Storable module, which was
    helpful to me).

    --
    David Filmer (http://DavidFilmer.com)
    , Sep 26, 2006
    #2
    1. Advertising

  3. <> wrote:
    > What's a better way to write the following:
    >
    > %hash = %{$hashref1->{"key"}};
    > $hashref2 = \%hash;
    >
    > Assuming $hashref1->{"key"} is a hashref I want to copy (not
    > reference), and %hash is never used.



    You can use an "anonymous hash constructor" instead of the %hash
    temporary variable.

    (I think. Can't give a real answer due to lack of real data...)


    > My best guess is:
    >
    > $hashref2 = \%{$hashref1->{"key"}};
    >
    > But that references $hashref1->{"key"} rather than copies it (ie doesnt
    > work).



    my $hashref2 = { %{$hashref1->{"key"}} };


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 26, 2006
    #3
  4. -berlin.de Guest

    Michele Dondi <> wrote in comp.lang.perl.misc:
    > On 26 Sep 2006 00:43:51 -0700, wrote:
    >
    > >Assuming $hashref1->{"key"} is a hashref I want to copy (not
    > >reference), and %hash is never used. My best guess is:
    > >
    > >$hashref2 = \%{$hashref1->{"key"}};

    >
    > No. Dereference and reference inverse functions. If you compose them
    > you obtain identity.


    ....with one exception. When $x is an object that overloads hash
    de-referencing \%$x is in general different from $x.

    Anno
    -berlin.de, Sep 27, 2006
    #4
  5. Guest

    wrote:
    > I'm not sure I understand your question, but I THINK you are asking how
    > you can copy an entire hash structure (ie, "deep" copy), and not just a
    > copy of the reference itself (ie, "shallow copy"). If this is your


    Yep, that's exactly it, thanks. Funny, I remember reading about deep vs
    shallow years ago, but I haven't programmed for so long I'm now
    fumbling around like newborn! It's just as much fun to learn the second
    time around :)

    SS
    , Sep 28, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    491
    red floyd
    Nov 10, 2011
  2. Une bévue
    Replies:
    5
    Views:
    140
    Une bévue
    Aug 10, 2006
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    595
    David A. Black
    Jul 2, 2008
  4. Steve

    hash reference as a hash key

    Steve, Sep 26, 2003, in forum: Perl Misc
    Replies:
    6
    Views:
    107
    Steve
    Sep 28, 2003
  5. Replies:
    14
    Views:
    229
    Tomi Häsä
    Jan 10, 2005
Loading...

Share This Page