ovedpo15 has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Converting hash structure into a special tree
by kschwab (Vicar) on Jan 13, 2019 at 14:20 UTC
    How about Data::TreeDumper?
    use Data::TreeDumper; $Data::TreeDumper::Maxdepth=4; $Data::TreeDumper::Useascii=1; $Data::TreeDumper::Displayaddress=0; my $d= { "one" => 1, "two" => 2, "array" => [1,2,3,4], "hash" => {"test" => "me"} }; print DumpTree($d,'Contents of $d');
    Outputs:
    Contents of $d
    |- array 
    |  |- 0 = 1 
    |  |- 1 = 2 
    |  |- 2 = 3 
    |  `- 3 = 4 
    |- hash 
    |  `- test = me 
    |- one = 1 
    `- two = 2 
    
      It looks like an amazing module! Sadly I can't use it because it is not installed (And I can't install new modules on my school project). I'm allowed to use about 500 modules but Data::TreeDumper is not one of them. :(
      EDIT: I saw that I have 'Tree::DAG_Node' module. I tried to read the docs but it does not do the format I need.
      I would try to convince my lecture to let me use that module. What structure do you recommend me to pass to the module? Can you give me a hint on how to implement it? I read the docs it feels straight forward but I'm not sure which hash format should I pass and which algorithm to implt in order to create one.
Re: Converting hash structure into a special tree
by tybalt89 (Monsignor) on Jan 13, 2019 at 19:43 UTC
    #!/usr/bin/perl # https://perlmonks.org/?node_id=1228464 use strict; use warnings; local $_ = join '', sort map "$_\n", map {join ',', reverse split /,/ +} map s/\.\/|\n//gr, <DATA>; sub transpose { my $tmp = ''; $tmp .= "\n" while s/^./ $tmp .= $&; '' /gem; $_ = $tmp; } my @p = map quotemeta, /[^,\s]+/g; my $lead = do { local $" = '|'; qr/(@p)/ }; 1 while s/^[-| ]*\K$lead,/|-- /m; my $line = ''; $line |= $_ for /.+/g; my $length = length $line; s/.+/ sprintf "%-${length}s", $& /ge; transpose; s/\|*\K\|/`/g; transpose; s/ +$//gm; s/--(?= [|`])/ /g; s/^/ /gm; s/ /`-- /; print; __DATA__ ./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

    Outputs:

    `-- hello_world.pl |-- bin-perl |-- bye.pl | |-- bin-perl | `-- strict.pm `-- strict.pm

      a prayer answered!

        > a prayer answered!

        Not sure if from heaven or hell ... ;)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Amazing! Problem is that I don't know most of that syntax (I guess it a perl pro level). I'll try to understand it and simplified it. Thank you
        You sure about the code? tried to run it and I get:
        `-- hello_world.pl ,bin-perl_world.pl ,bye.pllo_world.pl ,bye.pl,strict.pml ,strict.pmworld.pl ` `-- bin-perl
Re: Converting hash structure into a special tree
by Anonymous Monk on Jan 13, 2019 at 14:10 UTC
    At first, I have tried to find a module which does something similar but 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.

    so you have code but you dont want to show it? sounds like you want us to do your work for you

      I'm sorry, it does not the way I meant. I have deleted it because I feel like it is not the right direction. Sorry again.
Re: Converting hash structure into a special tree
by ovedpo15 (Pilgrim) on Jan 14, 2019 at 01:26 UTC
    Hi guys, So I found out a good Perl script in the following link: tree.pl - kinda like tree
    But it iterates through the list of directories and prints the format.
    Is it possible to work with it and make it get the processs_hash and files_hash and print the data?
    How does the array input of the DumpTree function should look like?