Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: out of order tree generation

by choroba (Cardinal)
on Apr 03, 2019 at 21:14 UTC ( [id://1232120]=note: print w/replies, xml ) Need Help??


in reply to out of order tree generation

Here's one way to build a nested hash whose structure corresponds to the tree:
my ($level2) = values %$VAR1; my @work = (my $root = { id => 0 }); while (@work) { my @next_work; for my $parent (@work) { my @children = grep $level2->{$_}{boss} == $parent->{id}, keys %$level2; $parent->{children} = [ map { id => $_, name => $level2->{$_}{ +name} }, @children ]; push @next_work, @{ $parent->{children} }; delete @$level2{@children}; } @work = @next_work; } use Data::Dumper; print Dumper($root);
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1232120]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 21:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found