in reply to Hashes and values
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" }
|
|---|