in reply to directory tree to hash of arrays
G'day mabossert,
It would have been a lot more helpful to show us your code and explain what parts you were having difficulty with.
You say you found a solution but had a problem understanding the code. If you told us what that problem was, we could help you with it.
The actual data structure you want to create is completely unclear. You say "What I want to do is have something like this ..." but what you show doesn't seem to equate with a tree structure indicated in the title ("directory tree to hash of arrays"). Perhaps look in "perldsc - Perl Data Structures Cookbook" and try to determine what you really want.
As it happens, just yesterday I wrote "Re: Recursive Directory print". I've made some minor modifications: this may do what you want.
#!/usr/bin/env perl use strict; use warnings; use autodie; my %dir_tree; my $root_dir = '.'; build_dir_tree(\%dir_tree, $root_dir); sub build_dir_tree { my ($tree, $path) = @_; opendir(my $dh, $path); for (readdir $dh) { next if /^(?:\.|\.\.)$/; if (-d "$path/$_") { $tree->{$_} = {}; build_dir_tree($tree->{$_}, "$path/$_"); } else { $tree->{$_} = -s "$path/$_"; } } } { use Data::Dumper; local $Data::Dumper::Indent = 1; local $Data::Dumper::Sortkeys = 1; print Dumper \%dir_tree; }
The output is almost 3,000 lines long so I won't post it here. Here's an extract to give you an idea of the structure it produces.
... 'pm_multi_line_match.pl' => 1025, 'pm_multi_mod_distro_test' => { 'My-Example' => { 'Build.PL' => 651, 'Changes' => 92, 'MANIFEST' => 136, 'Makefile.PL' => 730, 'README' => 1316, 'ignore.txt' => 120, 'lib' => { 'My' => { 'Example.pm' => 5155 } }, 't' => { '00.load.t' => 117, 'perlcritic.t' => 454, 'pod-coverage.t' => 437, 'pod.t' => 405 } } }, 'pm_multiline_parse.pl' => 835, 'pm_multiline_parse_hdb_prob.pl' => 601, ...
If that's what you want, then fine — just use it. If you have follow-up questions, please address my opening remarks and adhere to the guidelines in "How do I post a question effectively?".
And, in case you didn't know, the JSON module will do the conversion.
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: directory tree to hash of arrays
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 | |
by mabossert (Scribe) on Mar 14, 2014 at 06:11 UTC |