mabossert has asked for the wisdom of the Perl Monks concerning the following question:
Ladies and/or gents, I have found a previous solution that gets really close to what I need, but I guess I am just misunderstanding how it actually works. What I want is to be able to convert a directory structure to a hash or arrays such that it will be easily converted to JSON for presentation to a web application. Here.
UPDATE: I didn't want to delete the original post...so, here is where I am:
Here is the current code, which is not doing what I want it to, and am having trouble wrapping my head around how to get to the structure I want to get. First, the data structure I want:
my @dk = ( {data => [ { text => 'data', data => { spriteCssClass => 'folder', 'fullPath' => '/data' }, contents => [ { text => 'raw', data => { spriteCssClass => 'folder', fullPath => '/data/raw' }, contents => [ { text => 'file1.csv', data => { spriteCssClass => 'csv +', fullPath => '/data/raw +/file1.csv', size => '12345' } }, { text => 'file2.csv', data => { spriteCssClass => 'csv +', fullPath => '/data/raw +/file2.csv', size => '123' } } ] }, { text => 'rdf', data => { spriteCssClass => 'folder', fullPath => '/data/rdf' }, contents => [ { text => 'file1.rdf', data => { spriteCssClass => 'rdf +', fullPath => '/data/raw +/file1.rdf', size => '123456' } }, { text => 'file2.rdf', data => { spriteCssClass => 'rdf +', fullPath => '/data/raw +/file2.rdf', size => '1235' } } ] } ], } ] });
Now, the code...which is as far as I have gotten, modifying the previously mentioned code and reference Here.
sub _directoryTree { my ($directory,$filter) = @_; my (@files,@dirs); find(sub{ push @files, $File::Find::name; },$directory); foreach my $file(@files) { chomp $file; my $ref = \@dirs; my $truncated = $file; $truncated =~ s/^$filter//; foreach my $dir ( split /\//,$truncated ) { my $i = 0; next if $dir eq ''; $i++ while ( $ref->[$i] and $ref->[$i]{name} ne $dir ); my $r; if (-f $file) { my ($ext,$spriteclass); if ($file =~ /\.(\w+)$/) { $ext = $1; if ($ext eq 'html') { $spriteclass = 'html'; } elsif($ext =~ m/png|jpg|jpeg|gif|ico/) { $spriteclass = 'image'; } elsif($ext eq 'pdf') { $spriteclass = 'pdf'; } else { $spriteclass = 'html'; } } $r= $ref->[$i] ||= {'text' => $dir, 'data' => { 'spriteCssClass' => $spritecla +ss, 'fullPath' => $file, 'id' => $dir, 'size' => (-s $file), 'type' => $ext } } unless $dir =~ m/^\./; #, 'access +ed' => ctime(stat($file)->atime) } else { $r= $ref->[$i] ||= {'text' => $dir, 'files' => [], 'data' => { 'spriteCssClass' => 'folder', 'fullPath' => $file, 'id' => $dir, 'size' => 0, 'type' => 'folder' } }; } $ref = $r->{'files'}; } } return \@dirs; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: directory tree to hash of arrays
by Kenosis (Priest) on Mar 08, 2014 at 00:27 UTC | |
by mabossert (Scribe) on Mar 08, 2014 at 00:59 UTC | |
|
Re: directory tree to hash of arrays
by kcott (Archbishop) on Mar 08, 2014 at 01:09 UTC | |
by mabossert (Scribe) on Mar 08, 2014 at 01:28 UTC | |
by kcott (Archbishop) on Mar 08, 2014 at 02:17 UTC | |
by mabossert (Scribe) on Mar 08, 2014 at 04:23 UTC | |
by hippo (Archbishop) on Mar 08, 2014 at 10:29 UTC | |
by mabossert (Scribe) on Mar 14, 2014 at 02:18 UTC | |
by kcott (Archbishop) on Mar 14, 2014 at 04:36 UTC | |
|