Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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

by AnaximanderThales (Novice)
on Dec 25, 2015 at 23:11 UTC ( #1151169=note: print w/replies, xml ) Need Help??

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; }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1151169]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (1)
As of 2023-06-03 02:36 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (6 votes). Check out past polls.