For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > September 2006 > Reference to hash value









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Reference to hash value
schwarzenschafe@gmail.com

2006-09-26, 4:00 am

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

usenet@DavidFilmer.com

2006-09-26, 4:00 am

schwarzenschafe@gmail.com 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)

Mumia W. (reading news)

2006-09-26, 8:03 am

On 09/26/2006 02:43 AM, schwarzenschafe@gmail.com wrote:
> What's a better way to write the following:
>
> %hash = %{$hashref1->{"key"}};
> $hashref2 = \%hash;
> [...]


%hash = (key => $hashref1->{"key"});


--
paduille.4058.mumia.w@earthlink.net
Tad McClellan

2006-09-26, 6:59 pm

schwarzenschafe@gmail.com <schwarzenschafe@gmail.com> 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
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Michele Dondi

2006-09-26, 6:59 pm

On 26 Sep 2006 00:43:51 -0700, schwarzenschafe@gmail.com 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. I think you want

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


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{po
p^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
anno4000@radom.zrz.tu-berlin.de

2006-09-27, 7:59 am

Michele Dondi <bik.mido@tiscalinet.it> wrote in comp.lang.perl.misc:
> On 26 Sep 2006 00:43:51 -0700, schwarzenschafe@gmail.com wrote:
>
>
> 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
Michele Dondi

2006-09-27, 7:01 pm

On 27 Sep 2006 10:56:02 GMT, anno4000@radom.zrz.tu-berlin.de wrote:

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


Just another instance of Perl magic!


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{po
p^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
schwarzenschafe@gmail.com

2006-09-28, 3:59 am

usenet@DavidFilmer.com 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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com