On Feb 4, 2004, at 4:19 PM, David Byrne wrote:

Okay, sorry to be somewhat unclear with my question.
Here’s some sample input data that may help to clear
things up:

See if this gets you going. It's one possible answer.


James

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my $tree = data_to_hash_tree(); # build tree

print Dumper($tree); # print tree

sub data_to_hash_tree {
        my @nodes;              # to store nodes during reading

local $/ = ''; # use "paragraph" read mode

        while (<DATA>) {
                my %node = map { split /\s*=\s*/ } split /\n/;  # build node from data
                $nodes[$node{node_id}] = \%node;                                # add 
to list
        }

        foreach (@nodes) {              # cross reference and clean up nodes
                if ($_->{child_node_ids} eq 'N/A') { $_->{child_node_ids} = [ ]; }
                else {
                        $_->{child_node_ids} =
                                        [ map { $nodes[$_] } split /,\s*/, 
$_->{child_node_ids} ];
                }

                $_->{parent_node_id} = undef if $_->{parent_node_id} eq 'N/A';
                
        }
        
        return $nodes[0];       # return root
}

__DATA__
node_id = 0
parent_node_id = N/A
child_node_ids = 1, 2

node_id = 1
parent_node_id = 0
child_node_ids = 3, 4

node_id = 2
parent_node_id = 0
child_node_ids = 5, 6, 7

node_id = 3
parent_node_id = 1
child_node_ids = N/A

node_id = 4
parent_node_id = 1
child_node_ids = 8, 9

node_id = 5
parent_node_id = 2
child_node_ids = N/A

node_id = 6
parent_node_id = 2
child_node_ids = 10

node_id = 7
parent_node_id = 2
child_node_ids = N/A

node_id = 8
parent_node_id = 4
child_node_ids = N/A

node_id = 9
parent_node_id = 4
child_node_ids = 11, 12


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to