Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

RE : Copying a hash-of-hashes

> -----Message d'origine-----
> De=A0: Peter Rabbitson [mailto:rabbit@rabbit.us]
> Envoy=E9=A0: jeudi 30 d=E9cembre 2004 04:58
> =C0=A0: beginners@perl.org
> Objet=A0: Copying a hash-of-hashes
>=20
> 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:
>=20
> my %hoh =3D (
>             1 =3D> {
>                 a =3D> 5, b =3D> 5
>                 },
>=20
>             2 =3D> 5
>            );
>=20
>=20
> my @res =3D &RANDOMSUB;  #call a random subroutine
>=20
> print "$hoh{1}{a}  $hoh{2} \n";
>=20
>=20
> sub RANDOMSUB {
>     my %hohcopy =3D %hoh;
>=20
>     $hohcopy{1}{a} =3D 2;
>     $hohcopy{2} =3D 2;
> }
>=20 
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.
>=20
> 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.
>=20
> Thank you.
>=20

Hello,

This is well known behavior.
Try google on "deep copy" ...

On elegant way I know to do a "deep copy" in Perl
is to use dclone method of Storable module.

Something =E0 la:

use Storable qw(dclone);
my $hohcopy_ref =3D dclone \%hoh;

HTH,

Jos=E9.



Report this thread to moderator Post Follow-up to this message
Old Post
Jose Nyimi
12-30-04 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 08:43 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.