in reply to From: array To: complex hash
yields:use List::Util qw(reduce); use Data::Dumper; my @c = (1, 2, 3, 4); my %d; reduce { $a->{$b} ||= {} } \%d, @c; print Dumper \%d;
If the value of the terminating key needs to be something else than {}:$VAR1 = { '1' => { '2' => { '3' => { '4' => {} } } } };
which is slightly uglier, but will yield:(reduce { $a->{$b} ||= {} } \%d, @c[0..$#c-1])->{$c[$#c]} = 42; print Dumper \%d;
$VAR1 = { '1' => { '2' => { '3' => { '4' => 42 } } } };
|
|---|