more Data::Dumper trickery plus a more complicated input like demonstrated by TheDamian.

output:

Professor Amy Cubert Hermes Dwight Leela Bender Fry Zoidberg Satan Beelzebub Lucky Damien 'Satan Jr' Emacs LanX

use strict; use warnings; use Data::Dumper qw/Dumper/; use Data::Dump qw/pp/; #pp my $input = get_input(); my ( $tree, @roots ); while ( my ( $top, $second ) = each %$input ) { while ( my ( $id, $attr ) = each %$second ) { my $boss_id = $attr->{boss}; my $name = $attr->{name}; unless ($boss_id) { push @roots, $name; next; } my $boss_name = $second->{$boss_id}->{name}; $tree->{$boss_name}{$name} = $tree->{$name} //= {}; } } $Data::Dumper::Indent = 1; $Data::Dumper::Terse = 1; #$Data::Dumper::Pad = '#'; $Data::Dumper::Quotekeys = 0; #$Data::Dumper::Pair = ""; $Data::Dumper::Sortkeys = 1; my $graph = Dumper { map { $_ => $tree->{$_} } @roots }; $graph =~ s/[{},=>]//g; # delete hash symbols $graph =~ s/^\s*\n//gm; # delete empty lines print $graph; sub get_input { return { 666 => { 1 => { boss => 666, name => "Beelzebub" }, 11 => { boss => 245, name => "LanX" }, 99 => { boss => 666, name => "Damien" }, 245 => { boss => 333, name => "Emacs" }, 333 => { boss => 0, name => "Satan Jr" }, 666 => { boss => 0, name => "Satan" }, 777 => { boss => 1, name => "Lucky" }, }, 1929 => { 1 => { boss => 1929, name => "Hermes" }, 2 => { boss => 1, name => "Leela" }, 3 => { boss => 1929, name => "Amy" }, 4 => { boss => 1, name => "Zoidberg" }, 480 => { boss => 2, name => "Fry" }, 1919 => { boss => 2, name => "Bender" }, 1929 => { boss => 0, name => "Professor" }, 3968 => { boss => 1929, name => "Cubert" }, 4425 => { boss => 1, name => "Dwight" }, }, }; }

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


In reply to Re^2: out of order tree generation (Data::Dumper) by LanX
in thread out of order tree generation by adambot

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.