Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: how to construct tree from parent pointer list

by nothingmuch (Priest)
on Mar 22, 2006 at 09:54 UTC ( #538455=note: print w/ replies, xml ) Need Help??


in reply to how to construct tree from parent pointer list

Anonymous Monk's code can be slightly beautified:

use strict; use Data::Dumper; my %deep; { my %flat; while (<DATA>) { chomp; my ( $child, $parent ) = /^(.*?):(.*?)$/; # if the parent is new then it's possibly at the root of the struc +ture # like 'a' is unless ( exists $flat{$parent} ) { $flat{parent} = $deep{parent} = {}; } # find the parent, and mark this node as a child of it $flat{$parent}{$child} = ($flat{child} ||= {}) # since $child is a child of $parent it can't be at the root, so d +elete it if it's there delete $deep{$child}; }; }
-nuffin
zz zZ Z Z #!perl


Comment on Re: how to construct tree from parent pointer list
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2014-09-17 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (56 votes), past polls