sub hash { my ($ref, $hash) = @_; for my $el (@$ref) { $hash = $hash->{$el} ||= {}; } } my $h = {}; my @array = qw/ a b c d e /; hash( \@array, $h); print Dumper( $h );