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: > > ####################### > 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 > > #####...and so on ##### > > Notes: > - node_id = 0 is the root node. > - N/A means no further relationships exist. > - Only one parent exists for each child. > - The 'child_node_id' is redundant data. There will > be similar descriptive data for each node, but it > isnÂt important for the structure of the tree. > > Question: How can I create a tree (i.e. a hash of > hashes) given the above relationships?
although i haven't follow this thread close enough, the following seems to be what you want: #!/usr/bin/perl -w use strict; use Data::Dumper; my %v; my $tree; local $/ = ''; while(<DATA>){ my %t = map{y/ //d; split /=/} split /\n/; $v{$t{node_id}} = $t{parent_node_id}; for(grep !/n.a/i, split(/,/,$t{child_node_ids})){ my($p,@k) = $t{node_id}; while($p){ push(@k,$p); $p = $v{$p}; } eval '$tree->{' . join('}->{',reverse $_,@k,0) . '}={}'; } } print Dumper($tree); __DATA__ [data you provided above] __END__ given your above data, the script prints: $VAR1 = { '0' => { '1' => { '4' => { '8' => {}, '9' => { '11' => {}, '12' => {} } }, '3' => {} }, '2' => { '6' => { '10' => {} }, '7' => {}, '5' => {} } } }; david -- sub'_{print"@_ ";* \ = * __ ,\ & \} sub'__{print"@_ ";* \ = * ___ ,\ & \} sub'___{print"@_ ";* \ = * ____ ,\ & \} sub'____{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>