On Mon, Mar 28, 2005 at 03:06:41PM -0800, Zhuang Li wrote:
> Hi, given an array: @a = ('E1', 'E2', ..., 'En');
> Is there an easy way, hopefully one liner, to do the following without a
> loop? If not, will Perl support this in Perl 6?
>
> $hash->{E1}->{E2}->...->{En} = 1;
To read from such a series of hash keys is trivial:
$entry = $hash;
$entry = $entry->{$_} for @a;
print $entry;
To set it, you need to just do the same thing except that at each step
instead of keeping the hash element, you keep a reference to it:
$entryref = \$hash;
$entryref = \$$entryref->{$_} for @a;
$$entryref = 1;
Note that this will create hashrefs at any undefined levels, even the
top one.