use strict; use Data::Dumper; my %blocks; for (`du`) { (my $blocks, my $rest) = split; my @dirs = split /\//, $rest; my $where = \%blocks; while (@dirs) { $where = ($where->{shift @dirs} ||= {}); } $where->{""} = $blocks; } print Dumper(\%blocks);