use strict; use warnings; use Data::Dump::Streamer; my %hash; while () { chomp; my @elements = split /\s*\/\s*/; next if ! @elements; $hash{$elements[0]} = {} if ! exists $hash{$elements[0]}; my $subHash = $hash{shift @elements}; for (@elements) { $subHash->{$_} = {} unless exists $subHash->{$_}; $subHash = $subHash->{$_}; }; } Dump \%hash; __DATA__ one foo / bar foo / baz foo / qux / two foo / qux / three foo / qux / four five #### $HASH1 = { five => {}, foo => { bar => {}, baz => {}, qux => { four => {}, three => {}, two => {} } }, one => {} };