Reference to hash value

S

schwarzenschafe

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
 
U

usenet

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).
 
T

Tad McClellan

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"}} };
 
A

anno4000

Michele Dondi said:
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
 
S

schwarzenschafe

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top