<?xml version="1.0" encoding="windows-1252"?>
<node id="538455" title="Re: how to construct tree from parent pointer list" created="2006-03-22 04:54:34" updated="2006-03-21 23:54:34">
<type id="11">
note</type>
<author id="205152">
nothingmuch</author>
<data>
<field name="doctext">
Anonymous Monk's code can be slightly beautified:
&lt;c&gt;
use strict;
use Data::Dumper;
my %deep;

{
  my %flat;

  while (&lt;DATA&gt;) {
    chomp;

    my ( $child, $parent ) = /^(.*?):(.*?)$/;

    # if the parent is new then it's possibly at the root of the structure
    # 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 delete it if it's there
    delete $deep{$child};
  };
}
&lt;/c&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-205152"&gt;
-nuffin&lt;br&gt;zz zZ Z   Z #!perl
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
538326</field>
<field name="parent_node">
538326</field>
</data>
</node>
