use strict; use warnings; use Data::Dumper; my %hash; foreach my $line () #simulated simple CSV file { my @cols = (split /,|\n/,$line); my $href = \%hash; while (my $col = shift @cols) { $href->{$col} = {} unless (exists ($href->{$col}) and keys %{$href->{$col}} ); $href = $href->{$col}; } } print Dumper \%hash; =OUTPUT: $VAR1 = { 'x' => { '5' => { '6' => {} } }, 'a' => { 'x' => { 'e' => {}, 'm' => {}, 'c' => {} }, 'b' => { 'c' => {} }, 'y' => { 'h' => {} } } }; =cut __DATA__ a,x,c a,x,e a,y,h a,x,m a,b,c x,5,6