Consider the following hash structure:
$VAR1 = { '15746' => { 'ppid' => '15741', 'name' => 'echo Hello World' }, '15747' => { 'ppid' => '15741', 'name' => './bye.pl' }, '15741' => { 'ppid' => 'NA', 'name' => './hello_world.pl' }, '15748' => { 'ppid' => '15747', 'name' => 'echo Bye World' } };
Also consider the following structure of a paths:
$VAR1 = { 'data' => { './strict.pm' => { 'pid' => { '15747' => 1, '15741' => 1 } }, './hello_world.pl' => { 'pid' => { '15741' => 2 } }, './bye.pl' => { 'pid' => { '15747' => 2 } }, './bin-perl' => { 'pid' => { '15747' => 1, '15741' => 1 } } }, 'total' => { './strict.pm' => 6, './hello_world.pl' => 4, './bye.pl' => 4, './bin-perl' => 2 } };
In the previous thread I asked your help to create a chain file for each path. The final code looks as following:
sub parents { my ($name,$pid,$processes_href) = @_; unless(exists($processes_href->{$pid})) { return ''; } my $secondname = $processes_href->{$pid}{name}; if ($name eq $secondname) { return parents($secondname,$processes_href->{$pid}{ppid},$proc +esses_href); } return ",".$secondname.parents($secondname,$processes_href->{$pid} +{ppid},$processes_href); } sub create_chain { my ($processes_href,$paths_href) = @_; foreach my $data (keys(%{$paths_href>{'data'}})) { while (my ($file, $procs) = each %{$data}) { foreach my $pid (keys(%{$procs->{'pid'}})) { print $fh $file.parents($file,$pid,$processes_href)."\ +n"; } } } close($fh); }
Output:
./strict.pm,./hello_world.pl ./strict.pm,./bye.pl,./hello_world.pl ./hello_world.pl ./bye.pl,./hello_world.pl ./bin-perl,./hello_world.pl ./bin-perl,./bye.pl,./hello_world.pl
It works good but the final format is not so readable. I would like to create a format that looks like the output of the Linux command 'tree' which gets a directory and returns the recursive tree.
It looks as following:
tree ./regexpu-core/ |-- LICENSE-MIT.txt |-- README.md |-- data | |-- character-class-escape-sets.js | `-- iu-mappings.json |-- package.json `-- rewrite-pattern.js
For the example I gave, the output should be:
`-- hello_world.pl |-- bin-perl |-- bye.pl | |-- bin-perl | `-- strict.pm `-- strict.pm
At first, I have tried to find a module which does something similar but without any success. Also I tried to find some Linux command which does it but also without any success.
Now I'm trying to build a function which loops through the files creates each line, but for some reason it does not work. I'm not sure how to keep the length.
What would be a good and efficient solution do solve it? I copied the data exactly as printed by the Dummper :)
Also sorry if I have grammar mistakes. English is my third language.
Thank you

In reply to Converting hash structure into a special tree by ovedpo15

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.