Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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:

$r = $r->{$_} ||= {} for split m|/|, $tmp; #/ $dl{$name} ||= $r;
nor how the hash %root is being populated.

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, @
#!/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;
output -- $ENV{"HOME"} . "/bin/";
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',
        };

In reply to Using File::Find to create a hash of directory structure. by AnaximanderThales

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found