#!/usr/bin/perl # https://perlmonks.org/?node_id=1228107 use strict; use warnings; my $firsthash = { "./bye_world.pl" => { data => 4, pid => { 7302 => 2 } }, "./hello_world.pl" => { data => 4, pid => { 7287 => 2 } }, "/perl/5.14.1/strict.pm" => { data => 6, pid => { 7287 => 1, 7302 => 1 } }, "/some_text.txt/" => { data => 2, pid => { 7287 => 1, 7302 => 1 } }, }; my $secondhash = { 7287 => { name => "./hello_world.pl", parent => "###" }, 7299 => { name => "echo Hello World ", parent => 7287 }, 7302 => { name => "./bye_world.pl", parent => 7287 }, 7305 => { name => "echo Bye World ", parent => 7302 }, }; sub parents { my ($name, $pid) = @_; exists $secondhash->{$pid} or return ''; my $secondname = $secondhash->{$pid}{name}; return ($name ne $secondname and ",$secondname") . parents($secondname, $secondhash->{$pid}{parent}); } for my $name ( keys %$firsthash ) { for my $pid ( keys %{ $firsthash->{$name}{pid} } ) { print $name, parents($name, $pid), "\n"; } }