my $ptr = \%hash; $ptr = $ptr->{$_} = {} for @array[0..$#array-1]; # fixed; s/shift @array/$_/ $ptr->{$array[-1]} = 1;