use strict; my @array = qw/a b c d e f/; my $t = my $hashref = {}; while (@array) { $t = $t->{ shift @array } = @array>1 ? {} : 1 };