my %h; while (<>) { chomp; my @v = split; my $h = \%h; for ( @v ) { $h->{$_} ||= {}; $h = $h->{$_}; } }