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


In reply to Re: directory tree to hash of arrays by kcott
in thread directory tree to hash of arrays by mabossert

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.