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

Hello, I would like this cool shorthand to assign values to a hash slice, but I'm stumped at how to make this work in a hash of hashes. It may be simple, but I don't get it.
use Data::Dumper; @keys = qw(A B C); @values = qw(1 2 3);
The following works, of course:
@hash{@keys} = @values; print Dumper(\%hash);
but I would like the results of this clumsy code:
%hash=(); $hash{x}{A} = 1; $hash{x}{B} = 2; $hash{x}{C} = 3; print Dumper(\%hash);
... in a shorter and nicer syntax like the 1st example. Unfortunately, whatever I tried, it doesn't "do what I mean":
%hash=(); $hash{x}{@keys} = @values; print Dumper(\%hash);
gives me $hash{x}{3} == 3. Writing @hash{x}{@keys} = @values; or @{$hash{x}{@keys}} = @values; doesn't help. Thanks in advance for sharing your wisdom...

Replies are listed 'Best First'.
Re: how to $hash{key}{@keys} = @values
by dave_the_m (Monsignor) on Apr 10, 2005 at 16:11 UTC
      Thanks a lot! I thought I had tried all the possible incantations, but I had missed this one. It even makes sense, now that I see the solution.
        It more than makes sense, it's readily derivable. First, take the basic form of access you want, in this case a hash slice: @hash{@keys}, then replace the variable name (but leave the sigil) with an expression for the reference in curly braces: hash becomes {$hash{key}}, leaving @{$hash{key}}{@keys}.

        I'd recommend taking a peek at References quick reference.