Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Copying multi-level hash

by mt2k (Hermit)
on Mar 08, 2001 at 03:46 UTC ( [id://62870]=perlquestion: print w/replies, xml ) Need Help??

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

Help, I am hopelessly confused!!

Lets say that I have the following hash:

%hash = ( 'key1' => { 1 => { 'var' => 1, 1 => { 'name1' => 'value3', 'name2' => 'value2', 'name3' => 'value3' } }, 2 => { 'var' => 1, 1 => { 'name1' => 'value1', 'name2' => 'value2', 'name3' => 'value3' } }, } );

Now, how do I copy one part of this hash, such as the %hash{'key1'}{1} section??
Would it be done with one of these?:

1. %copy = %hash{'key1'}{1} 2. %copy = %{$hash{'key1'}{1}}
I really have no clue... I have just begun last week working with complex hashes and arrays, and I'm just a little confused! %-|

Replies are listed 'Best First'.
(jeffa) Re: Copying multi-level hash
by jeffa (Bishop) on Mar 08, 2001 at 03:53 UTC
    This worked just fine for me:
    my %new = %{$hash{key1}{1}};
    it produced:
    $VAR1 = 'var'; $VAR2 = 1; $VAR3 = 1; $VAR4 = { 'name1' => 'value3', 'name2' => 'value2', 'name3' => 'value3' };
    the extra '1' key is there because I was lazy and just eval'ed your Dumper output without 'use strict' . . . .

    short answer, take a number 2 :)

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: Copying multi-level hash
by merlyn (Sage) on Mar 08, 2001 at 03:58 UTC
Re: Copying multi-level hash
by mt2k (Hermit) on Mar 08, 2001 at 03:59 UTC
    Oh, and also, now if I wanted to place the %copy hash back in the big one (just theoretically), how would I do that?

    1. %hash{'key1'}{1} = %{$copy}; 2. %hash{'key1'}{1} = %copy; 3. $hash{'key1'}{1} = %copy; 4. %{$hash{'key1'}{1}} = %copy; 5. %{$hash{'key1'}{1}} = %{$copy};
    Though I somehow think none of those look right...
      #3 was close
      $hash{key1}{1} = \%copy;

      Jeff

      R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
      L-L--L-L--L-L--L-L--L-L--L-L--L-L--
      

      Remember above all : a reference is a scalar value, so reference it as you would a string (e.g.) -- as long as you remember it's pointing to something, instead of containing the value you want. Since you want to set it to a hashref:

      So:

      $hash{key1}{1} = \%copy;

      should do ya.

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Copying multi-level hash
by mt2k (Hermit) on Mar 08, 2001 at 04:15 UTC
    Crap, that's all wrong...
    I don't want to create a reference, I want to copy the hash section...
    Sorry if I wasn't very clear on that...

    I know to get rid of the backslash in that one place, but I have no idea what to do with the %{$hash} part

      Give the way you get:   ;-)
      %temp = %{$hash{key1}{1}}; then later: %{$hash{key1}{1}} = %temp;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-24 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found