Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Hashes and values

by Nansh (Acolyte)
on Jun 14, 2017 at 10:12 UTC ( [id://1192784]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

Can anybody tell me how to assign one hash as a value of other hash

Example: %hash1 and %hash2 are two hash

%hash1 should have %hash2 as its value

Thanks in advance.

Replies are listed 'Best First'.
Re: Hashes and values (updated)
by haukex (Archbishop) on Jun 14, 2017 at 10:18 UTC

    I think you're asking about a hash of hashes? In that case, here's one way:

    my %hash1 = ( foo => 'bar' ); my %hash2 = ( quz => 'baz' ); $hash1{somekey} = \%hash2; use Data::Dumper; print Dumper(\%hash1); print Dumper( $hash1{somekey}{quz} ); __END__ $VAR1 = { 'foo' => 'bar', 'somekey' => { 'quz' => 'baz' } }; $VAR1 = 'baz';

    See also perlreftut and perlref.

    Update: On re-reading your question, are you just asking for how to copy %hash2 into %hash1? Below, in "Case 1", the contents of %hash1 are preserved, but if there are duplicate keys in %hash1 and %hash2 then the values from %hash2 will replace those from %hash1. In "Case 2", the contents of %hash1 are simply replaced by %hash2. There are other ways to do this depending on what your needs are.

    use Data::Dump; my %hash1 = ( foo => 'bar' ); my %hash2 = ( quz => 'baz' ); # Case 1 %hash1 = (%hash1, %hash2); # OR $hash1{$_} = $hash2{$_} for keys %hash2; dd \%hash1; # { foo => "bar", quz => "baz" } # Case 2 %hash1 = %hash2; dd \%hash1; # { quz => "baz" }
Re: Hashes and values
by Discipulus (Canon) on Jun 14, 2017 at 10:42 UTC
    Hello Nansh,

    many way are possible (see docs linked by haukex), normally I use HoH created with the contained hash as anonymous one:

    my %container_hash; $container_hash{ holder_key } = { anonymous => 'hash', created_by => ' +braces' };

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Hashes and values
by perldigious (Priest) on Jun 14, 2017 at 13:24 UTC

    Hi Nansh,

    I'm hoping this node where I was trying to deal with a 3D hash (hash of hashes of hashes) is also helpful if you start dealing with more complex data structures, and especially if you start making use of slices to manipulate them. Actually, the entire thread is full of good information from lots of monks and I go back to it myself for a refresher every now and again when I'm doing such things because I sometimes have trouble correctly dereferencing what I want out of complex data structures (I have the link as a comment in several pieces of code I've written).

    Just another Perl hooker - But the purist monks keep telling me I should do it for love, not money.
Re: Hashes and values
by AnomalousMonk (Archbishop) on Jun 14, 2017 at 13:47 UTC

    Another link that may offer some insight on the question of "one hash in another hash" (if that's your question) is the Perl Data Structures Cookbook (perldsc).


    Give a man a fish:  <%-{-{-{-<

Re: Hashes and values
by sundialsvc4 (Abbot) on Jun 14, 2017 at 13:15 UTC

    One thing to keep in mind as you study this aspect of Perl is the notion of references, as discussed in the two perldoc pages you were given earlier.   Read these slowly and carefully, because “references” are used a lot when dealing with hashes.   (In fact, a so-called “hashref” is more-common than an actual variable that is a hash.   References are very versatile.   The contents of a hash are often references, this being the actual mechanism that gives us “hash of hashes” and other arbitrarily-complex data structures.)

    A reference is a value that “refers to” another piece of data ... a piece of data which might be “anonymous” in that the only way to reach it is through a reference.   More than one reference can be made to the same piece of data, and your troubles will begin if you don’t realize that this is what Perl is actually doing.   This can lead to confusion like:   “Why did $b also change, when I only meant to change $a?”   (Answer: “because both variables are references to the same piece of data ... whether you intended for them to be or not.”)   If there is any one thing that confused me the most when I was getting started with this language, this was probably it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2025-06-15 09:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.