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

[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