use strict; use warnings; use Data::Dump qw(pp); my @a = qw(now is the time for all good men); my %h; add_val(\%h, 'FOO', @a); print pp(\%h); sub add_val { my ($hashref, $val, @keylist) = @_; die "Expected a list of keys!" unless @keylist; die "Expected a hashref!" unless "HASH" eq ref $hashref; while (@keylist > 1) { my $key = shift @keylist; $hashref->{$key} = {} if ! exists $hashref->{$key}; $hashref = $hashref->{$key}; } $hashref->{shift @keylist} = $val; }