Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
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 meditating upon the Monastery: (7)
As of 2015-07-05 15:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls