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

Hello, I need a quick way to add a hash as the value to another hash in order to build a multi-dimensional hash. I tried these but they don't seem to work. Tried google and searching here but couldn't find quick solutions.
$hoh{'key'} = %hash; #doesn't work $hoh{'key'} = \%hash; #doesn't work
Thank you.

Replies are listed 'Best First'.
Re: Add hash to hash
by bichonfrise74 (Vicar) on Oct 22, 2009 at 22:38 UTC
    Here's a sample that shows your second line actually works.
    #!/usr/bin/perl use strict; use Data::Dumper; my %hoh; my %hash = ( 'a' => 1, 'b' => 2 ); $hoh{'key'} = \%hash; print Dumper \%hoh;
    The output is:
    $VAR1 = { 'key' => { 'a' => 1, 'b' => 2 } };
Re: Add hash to hash
by ikegami (Patriarch) on Oct 22, 2009 at 22:33 UTC

    You can't place a hash in a hash, only scalars. References are scalars, so you can place a reference to a hash in a hash. That's what the second line does. Contrary to your claims, it does work.

    perllol

      thank you ikegami. Your explanation was what I thought, but I could not get it to work on paper. Upon further review of my code, I have other problems causing me to think this was the problem.