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


In reply to Re: Reorganizing hash by LanX
in thread Reorganizing hash 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.