create_proc_tree(\%process_data,\%files_data); sub create_proc_tree { my ($proc_href,$files_href) = @_; my %hash; while (my ($file, $procs) = each %{$files_href}) { foreach my $pid (keys(%{$procs->{'pid'}})) { my $prev_file = $file; do { my $parent_id = $proc_href->{$pid}{parent}; my $parent_name = $proc_href->{$pid}{name}; if ($prev_file eq $parent_name) { $hash{$parent_name} = 1; } else { $hash{$parent_name} = { (%{ $hash{$prev_file} // {} }, $prev_file) }; delete($hash{$prev_file}); } $prev_file = $parent_name; my $parent_name = $proc_href->{$parent_id}{name}; } while(defined($parent_name)); } } print Dumper(\%hash); # Printing for debug }