in reply to Reorganizing hash

The following code creates a clean merger of the two hashes as a tree ( or rather directed graph) structure, i.e. it's not assuming that the input is not buggy.

The resulting merger can easily be post processed to create the desired output.

FWIW:

Instead of an recursive algorithm, I used the power of cross-references and auto-vivification.

This allows to handle possible loops in the input, without risking a non-halting recursive call.

use strict; use warnings; use Data::Dump qw/pp dd/; my %pid_tree; my %process_data = ( '56061' => { 'parent' => 'NA', 'name' => 'file1' }, '56069' => { 'parent' => '56065', 'name' => 'echo Hello_file1' }, '56062' => { 'parent' => '56061', 'name' => 'echo Hello_file2' }, '56065' => { 'parent' => '56061', 'name' => 'file2' } ); while ( my ($pid,$h_pid) = each %process_data ) { my ($parent,$name) = @{$h_pid}{qw/parent name/}; $pid_tree{$pid}{name} = $name; $pid_tree{$pid}{pid} = $pid; push @{ $pid_tree{$parent}{children} } ,$pid_tree{$pid}; } $pid_tree{NA}{name}= undef; $pid_tree{NA}{pid}= "NA"; my %files_data = ( './GetOpt.pm' => { 'pid' => { '56061' => 1, '56065' => 1 } }, 'file1' => { 'pid' => { '56061' => 2 } }, 'file2' => { 'pid' => { '56065' => 2 } }, './src/bin/perl' => { 'pid' => { '56061' => 1, '56065' => 1 } } ); while ( my ($file,$h_file) = each %files_data ){ while ( my ($attr, $h_attr) = each %$h_file ){ while ( my ($pid, $pid_count) = each %$h_attr ) { #print "$file $attr $pid $pid_count\n"; next if $file eq $pid_tree{$pid}{name}; push @{$pid_tree{$pid}{files}},$file } } } pp $pid_tree{NA};

OUTPUT

{ children => [ { children => [ { name => "echo Hello_file2", pid => 56062 }, { children => [{ name => "echo Hello_file1", pid => 56069 }], files => ["./src/bin/perl", "./GetOpt.pm"], name => "file2", pid => 56065, }, ], files => ["./src/bin/perl", "./GetOpt.pm"], name => "file1", pid => 56061, }, ], name => undef, pid => "NA", }

NB: I had to repair corrupted input from the OP. Again.

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