punkish has asked for the wisdom of the Perl Monks concerning the following question:
My brain is tired from staring at this. I need your sharp eyes to tell me what I am doing wrong. I am working on the basis of a tutorial on relational modeling of trees and graphs (see http://www.oreillynet.com/lpt/a/2958), aka "preorder traversal." I have the following data
table: child_node_id parent_node_id left_id right_id ------------- -------------- ------- -------- 2 1 3 1 4 1 5 1 6 2 7 2 8 4 9 5 10 7 9 10 my $children = $dbh->prepare(qq{ SELECT child_node_id FROM table WHERE parent_node_id = ? }); my $setleft = $dbh->prepare(qq{ UPDATE table SET left_id = ? WHERE child_node_id = ? }); my $setright = $dbh->prepare(qq{ UPDATE table SET right_id = ? WHERE child_node_id = ? }); my $ctr = 1; my $rootid = 1; walktree($rootid); sub walktree { my ($id) = @_; $setleft->execute($ctr++, $id); $children->execute($id); while(my ($subid) = $children->fetchrow_array) { walktree($subid); } $setright->execute($ctr++, $id); }
However, the code shown above does not work. It starts off with
Update: Solved. See replies from gamache and blokhead below.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: walking a tree
by gamache (Friar) on Nov 13, 2007 at 03:46 UTC | |
|
Re: walking a tree
by blokhead (Monsignor) on Nov 13, 2007 at 03:48 UTC | |
|
Re: walking a tree
by dragonchild (Archbishop) on Nov 13, 2007 at 14:54 UTC |