#!/usr/bin/perl -w use Data::Dumper; {my $i=1; sub makevalue { return $i++; } } my $hash={};my $h0; my $l;my @c; while ($l=) { $h0=$hash; @c=split '/',$l; for my $c (@c[0..$#c-1]) { next unless $c; $h0=($h0->{$c}||={}); } $h0->{$c[-1]}=makevalue($l); } print Dumper($hash); __DATA__ /root/dir1/file1 /root/dir1/file2 /root/dir1/file3 /root/dir2/file1