[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

RE: [FWP] Re: Copying a hash of hashes



FWPers:

Here's the easier way ... Just looked at the Perl Cookbook, it says this (in
essence) about "deep copy":

use Storable;
my $d = dclone(\%yourhash);
#-------------------
Also, this would likely work...

#!/usr/bin/perl -w
use strict;
use Data::Dumper;

#completely untested, there's GOTTA be something wrong with this...
my %hash = ( ... your complex hash here ... }
my $text = Data::Dumper(\%hash);
my %newhash = %(eval {%text} );  
# we do an 'eval' on the data dumper representation of the hash....

joshr

----------------------------------------------------
Josh Rabinowitz - Software Engineer - joshr@cnet.com 

> -----Original Message-----
> From: sthoenna@efn.org [mailto:sthoenna@efn.org]
> Sent: Tuesday, August 08, 2000 4:15 PM
> To: mjd-clpm-submit@plover.com
> Cc: fwp@technofile.org; sbl+news@dd.chalmers.se
> Subject: [FWP] Re: Copying a hash of hashes
> 
> 
> In article <slrn8ovnra.h9i.sbl+news@gimli.dd.chalmers.se>,
> sbl+news@dd.chalmers.se (Stefan Berglund) wrote:
> > How do I copy all members of a hash of hashes to a new hash?
> > 
> > Is the only way to iterate over the hash copying each 
> member depending on it's
> > type? If so is it possible to find out what type a specific 
> variable has?
> 
> Here's one way:
> 
> # deepcopy does a deep copy of arrays or hashes nested in 
> what's passed to it.
> # scalar refs or objects or simple scalars will be simply copied
> 
> sub deepcopy($);
> sub deepcopy($)
> {
>    if (ref $_[0] eq 'HASH') {
>       return { map(deepcopy($_), %{$_[0]}) };
>    } elsif (ref $_[0] eq 'ARRAY') {
>       return [ map(deepcopy($_), @{$_[0]}) ];
>    }
>    $_[0]
> }
> 
> ==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
> ==== Send email to <fwp-request@technofile.org> with message _body_
> ====   unsubscribe
> 

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe