http://www.perlmonks.org?node_id=1151169


in reply to Using File::Find to create a hash of directory structure.

Thank you Discipulus and the Anonymous Monk(s).

I was at least able to comment the code as to what it was doing, and I only needed to make some slight modifications to my code to make it work as I Wanted it to work.

I'll just drop in the subroutine and make notes directly on it.

sub parse_tree { # note:make sure trailing slash is removed from passed arg my ($root_path) = @_; my %root; my %dl; my %count; # This is the wanted for find. my $path_checker = sub { # grab item name my $name = $File::Find::name; # If directory -- if (-d $name ) { # Assign current root hash as reference to r my $r = \%root; # hold the item in temp my $tmp = $name; # Remove the root path section of the file to get rid of the # first part of the path $tmp =~ s/^\Q$root_path\E//; # If a leading '/' is left, remove that. $tmp =~ s/^\///; # if the entry in the hash is undefined, create a new empty # hash from the splitted path of tmp. $r = $r->{$_} ||= {} for split m|/|, $tmp; #/ # if $dl{$name} is undefind, the assign $r to $dl{$name}. $dl{$name} ||= $r; } elsif (-f $name) { # If $name is a file, find the directory path my $dir = $File::Find::dir; # increment the file count based on that directory. my $key = "file". ++$count{ $dir }; # create a new entry in the hash $dl{$dir}{$key} = $_; } }; find($path_checker, $root_path); return \%root; }