diotalevi has asked for the wisdom of the Perl Monks concerning the following question:
I assume many of you are used to doing logic programming in perl but this is new to me. I wrote up a simple optree-recursing (this won't work in practice because of NULL nodes) expression which finds a given node in a tree and returns the path required to reach it. Is there a common way to write a more plausible implementation of $ROOT->(parent|sibling)* $target that doesn't require a regex to handle all the bookkeeping of tracking successes, failures and to-do work?
sub path_to_op { my ( $root, $tgt ) = @_; local $op = $root; local @path; '' =~ /(?: (?(?{ my $name = $op->oldname; local $op = $op->parent; local @path = ( @path, "$name/" ); })(?=)|(?!)) | (?(?{ my $name = $op->oldname; local $op = $op->sibling; local @path = ( @path, "...," ); })(?=)|(?!)) ) * (?(?{ $$op == $$tgt })(?=)|(?!)) /x; @path; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Logic programming without using the RE engine as a crutch
by demerphq (Chancellor) on Jun 30, 2004 at 17:36 UTC | |
by diotalevi (Canon) on Jun 30, 2004 at 17:38 UTC | |
by demerphq (Chancellor) on Jun 30, 2004 at 17:48 UTC | |
by Anonymous Monk on Jul 02, 2004 at 15:20 UTC | |
by Anonymous Monk on Jul 02, 2004 at 15:23 UTC | |
|
Re: Logic programming without using the RE engine as a crutch
by Zaxo (Archbishop) on Jun 30, 2004 at 17:42 UTC |