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

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



>>>>> "Yitzchak" == Yitzchak Scott-Thoennes <sthoenna@efn.org> writes:

Yitzchak> In article <slrn8ovnra.h9i.sbl+news@gimli.dd.chalmers.se>,
Yitzchak> 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?

Yitzchak> Here's one way:

Yitzchak> # deepcopy does a deep copy of arrays or hashes nested in what's passed to it.
Yitzchak> # scalar refs or objects or simple scalars will be simply copied

Yitzchak> sub deepcopy($);
Yitzchak> sub deepcopy($)
Yitzchak> {
Yitzchak>    if (ref $_[0] eq 'HASH') {
Yitzchak>       return { map(deepcopy($_), %{$_[0]}) };
Yitzchak>    } elsif (ref $_[0] eq 'ARRAY') {
Yitzchak>       return [ map(deepcopy($_), @{$_[0]}) ];
Yitzchak>    }
Yitzchak>    $_[0]
Yitzchak> }

Also see 

        <http://www.stonehenge.com/merlyn/UnixReview/col30.html>

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

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