my %hash; my @path = (5, 6); my $ref = $hash{root}{1} ||= {}; $ref = $$ref{$_} ||= {} for @path; $$ref{2} = "b"; use YAML; print Dump \%hash; __END__ --- root: 1: 5: 6: 2: b