Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Copying hash contents, from nested hash (was: a simple one)

by Anonymous Monk
on Nov 03, 2002 at 12:40 UTC ( #210043=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks basically i have a nested hash of type
$hash{$cookie}{$service}->[0];
here for every cookie there are many services
what i want to do is copy the the
contents of every cookie(ie a hash of service and values)
to another hash
can u help me

update (broquaint): title change

Replies are listed 'Best First'.
Re: Copying hash contents, from nested hash (was: a simple one)
by broquaint (Abbot) on Nov 03, 2002 at 13:39 UTC
    If you want deep clone of every cookie then you can just use the dclone method from Storable e.g
    my %newhash = %{ dclone \%hash };
    Or a copy per cookie
    foreach (values %hash) { my %newcookie = %{ dclone $_ }; # more code here }

    HTH

    _________
    broquaint

Re: a simple one
by Tanalis (Curate) on Nov 03, 2002 at 13:05 UTC
    So, basically, you need to rehash the hash you have on service, and assign the value to it?

    The way I'd go about doing that (I'm sure there's a better way) would be something like .. (untested code)

    my $servicehash; foreach my $cookie (keys %hash) { foreach my $service (keys %{$hash{$cookie}}) { $servicehash{$service} = $hash{$cookie}{$service}; } }

    That should leave you with a hash of values keyed on service.

    Hope that helps ..
    --Foxcub

Re: a simple one
by fruiture (Curate) on Nov 03, 2002 at 13:00 UTC

    `perldoc perlref` will help you.

    %hash = ( 'a' => { 'aa' => [0,1,2] , 'ab' => [3,4,5] }, 'b' => { 'ba' => [6,7,8] , 'bb' => [9,10,11] }, ); my %a_hash = %{ $hash{a} };
    --
    http://fruiture.de
•Re: Copying hash contents, from nested hash (was: a simple one)
by merlyn (Sage) on Nov 03, 2002 at 14:04 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://210043]
Approved by broquaint
Front-paged by wil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2021-09-27 19:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?