my $hashref = {}; my $ref = \$hashref; for (@foo) { $ref = \$$ref->{$_}; } $$ref = 1; # or the value you want it to be print Dumper $hashref;