ovedpo15 has asked for the wisdom of the Perl Monks concerning the following question:
to a file with the following format: id,parent-id,name.15,10,name3 10,#####,name1 12,10,name2 5,12,name4 8,5,name4
output:my %data = ( '10517' => { 'parent' => '10516', 'start' => 1545321095, 'end' => 1545321098, 'name' => 'A' }, '10515' => { 'parent' => '10513', 'start' => 1545321091, 'end' => 1545321095, 'name' => 'B' }, '10514' => { 'parent' => '10513', 'start' => 1545321091, 'end' => 1545321095, 'name' => 'C' }, '10516' => { 'parent' => '10513', 'start' => 1545321091, 'end' => 1545321095, 'name' => 'D', }, '10511' => { 'parent' => '#####', 'start' => 1545321090, 'end' => 1545321099, 'name' => 'E' }, '10513' => { 'parent' => '10511', 'start' => 1545321091, 'end' => 1545321097, 'name' => 'F' }, ); foreach my $pid (sort(keys(%data))) { my @parent_list; my $temp_id = $pid; while (exists $data{$temp_id}) { push (@parent_list, $data{$temp_id}{name}); $temp_id = $data{$temp_id}{parent}; } my $line = join("\t|||\t",@parent_list)."\n"; print $line; }
It works great! it clear and readable (at least for me), and it does not use any additional modules.E F ||| E C ||| F ||| E B ||| F ||| E D ||| F ||| E A ||| D ||| F ||| E
$VAR1 = { 'A' => { 'pid' => { '5' => 2 }, 'data' => 4 }, 'B' => { 'pid' => { '8' => 1 '12' => 3 }, 'data' => 3 }, 'C' => { 'pid' => { '9' => 3 }, 'data' => 18 }, 'D' => { 'pid' => { '3' => 3 }, 'data' => 15 }, }
$VAR1 = { '8' => { 'parent' => '5', 'name' => 'full_B' }, '9' => { 'parent' => '5', 'name' => 'full_C' }, '5' => { 'parent' => '#####', # No parent 'name' => 'full_A' }, '10' => { 'parent' => '8', 'name' => 'full_D' }, '12' => { 'parent' => '8', 'name' => 'full_D' }, }
I would like to create a file which contains: for each one of the files in the first hash, I would like to create a the chain to get to the '###' main parent.<id>,<parent>,<name> 5,###,full_A 8,5,full_B 9,5,full_C 10,8,full_D
I just iterated through the files and checked each parent id.A B,A C,B,A
Second hash:$VAR1 = { '/perl/5.14.1/strict.pm' => { 'pid' => { '7302' => 1, '7287' => 1 }, 'data' => 6 }, './hello_world.pl' => { 'pid' => { '7287' => 2 }, 'data' => 4 }, './bye_world.pl' => { 'pid' => { '7302' => 2 }, 'data' => 4 }, '/some_text.txt/' => { 'pid' => { '7302' => 1, '7287' => 1 }, 'data' => 2 } };
Expected output:$VAR1 = { '7299' => { 'parent' => '7287', 'name' => 'echo Hello World ' }, '7305' => { 'parent' => '7302', 'name' => 'echo Bye World ' }, '7302' => { 'parent' => '7287', 'name' => './bye_world.pl' }, '7287' => { 'parent' => '###', 'name' => './hello_world.pl' } };
./hello_world.pl /perl/5.14.1/strict.pm,./hello_world.pl /perl/5.14.1/strict.pm,./bye.pl,./hello_world.pl ./bye_world.pl,./hello_world.pl some_text.txt,./hello_world.pl some_text.txt,./bye_world.pl,./hello_world.pl
2. Combine the function with already existing function.<file1>,<chain-file(N-1)>,<chain-file(N-1)>,...,<chain-file1><br>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Iterating through process hierarchy
by tybalt89 (Monsignor) on Jan 06, 2019 at 19:09 UTC |