AnaximanderThales has asked for the wisdom of the Perl Monks concerning the following question:
Oh Holy Monks, I seek thy wisdom.
Below is my code and output. My attempt is to create a hash of my directory listing. The parse_tree sub does exactly that, but I'm not happy with the results. I found this function from -- er, I believe here -- though it could have been SO.
Regardless, my issue is with the way it's handling the root path. I do see a difference when I exclude the trailing '/' on my path, however an issue still remains.
My issue is with the '' => 'home' => 'user' => 'bin' structure. If I exclude the trailing '/', the files under there are in the root of the hash, but then all folders fall under '' => 'TEST', etc.
My additional issue is that I have not been able to figure out what the parse_tree function is actually doing. I'm just not skilled enough to understand these lines:
nor how the hash %root is being populated.$r = $r->{$_} ||= {} for split m|/|, $tmp; #/ $dl{$name} ||= $r;
my goal is to have the data appear like this with Dumper:
$VAR1 = {
'file1' => 'file01',
'file2' => 'file02'
TEST => {
'file1' => 'file01'
}
test => {
'file3' => 'file03',
subfolder => {
'file1' => 'file01'
}
'file2' => 'file02',
'file1' => 'file01'
}
}
}
I'd like it this way because I won't necessarily know the directory that's being queried, and while I could certainly find more code and make it look like that, I'd prefer to understand what this is doing, and get ideas on what I need to do to get the format the way I'd like to see it from dumper. This will help me learn, and will also allow me to put comments on the code that i understand for when I look at this later on.
Thank you for your time,
@
output -- $ENV{"HOME"} . "/bin/";#!/usr/bin/perl # # Initial perl settings -- Using strict and warning # to ensure I'm writing proper perl. use strict; use warnings; use diagnostics; use Data::Dumper; use File::Find; my $dirHash = {}; # Hash file of directory my $testDir = $ENV{"HOME"} . "/bin/"; # Grab a list of files and put into hash $dirHash = parse_tree("$testDir"); # Dump -- for testing print "directory hash dump\n" . Dumper $dirHash; # Functions sub parse_tree { my ($root_path) = @_; my %root; my %dl; my %count; my $path_checker = sub { my $name = $File::Find::name; if (-d $name ) { my $r = \%root; my $tmp = $name; $tmp =~ s/^\Q$root_path\E//; $r = $r->{$_} ||= {} for split m|/|, $tmp; #/ $dl{$name} ||= $r; } elsif (-f $name) { my $dir = $File::Find::dir; my $key = "file". ++$count{ $dir }; $dl{$dir}{$key} = $_; } }; find($path_checker, $root_path); return \%root; } exit 0;
directory hash dump
$VAR1 = {
'' => {
'home' => {
'user' => {
'bin' => {
'file2' => 'results.txt',
'file5' => 'rsync_logs',
'file1' => 'sdu.sh',
'file3' => 'parse_tree_test.pl',
}
}
}
},
'TEST' => {
'file1' => 'file05',
'file2' => 'file03',
'file4' => 'file04',
'file3' => 'file02',
'file5' => 'file01'
},
'test' => {
'file1' => 'file02',
'file2' => 'file01',
'subtest' => {
'file2' => 'file06',
'file1' => 'file05'
}
}
};
output -- $ENV{"HOME"} . "/bin";
directory hash dump
$VAR1 = {
'' => {
'TEST' => {
'file1' => 'file05',
'file2' => 'file03',
'file4' => 'file04',
'file3' => 'file02',
'file5' => 'file01'
},
'test' => {
'file1' => 'file02',
'file2' => 'file01',
'subtest' => {
'file2' => 'file06',
'file1' => 'file05'
}
'file2' => 'results.txt',
'file5' => 'rsync_logs',
'file1' => 'sdu.sh',
'file3' => 'parse_tree_test.pl',
};
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using File::Find to create a hash of directory structure.
by Discipulus (Canon) on Dec 19, 2015 at 22:20 UTC | |
by Anonymous Monk on Dec 19, 2015 at 22:53 UTC | |
by AnaximanderThales (Novice) on Dec 21, 2015 at 21:07 UTC | |
by AnaximanderThales (Novice) on Dec 21, 2015 at 21:06 UTC | |
|
Re: Using File::Find to create a hash of directory structure.
by AnaximanderThales (Novice) on Dec 25, 2015 at 23:11 UTC | |
|
Re: Using File::Find to create a hash of directory structure.
by Anonymous Monk on Dec 19, 2015 at 22:11 UTC | |
by AnaximanderThales (Novice) on Dec 21, 2015 at 21:13 UTC |