in reply to Logic programming without using the RE engine as a crutch

Maybe im totally missing the point, but the following will traverse a tree composed of hashes and return the path.

sub path_to_op { my ($node,$tgt,$path)=@_; $path||=''; return $path if $node==$tgt; foreach my $key (keys %$node) { my $path=path_to_op($node->{$key},$tgt,"$path{$key}"); return $path if $path; } return '' }

But this seems so blindingly obvious to me that I'm having trouble believing I understand the question. After all I know you are very competent programmer so im at a loss to accept that I understand the question correctly....


---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi


Replies are listed 'Best First'.
Re^2: Logic programming without using the RE engine as a crutch
by diotalevi (Canon) on Jun 30, 2004 at 17:38 UTC
    Oh uh. Yeah. Heh.

      Sounds like you got yourself stuck in something that I believe psychologistics call "functional fixidity" you were preconditioned (probably by reading too many nodes by abigail-ii ;-) that you forgot that tree traversal is a simple thing.

      Anyway I offer this little perspective to maybe help you avoid such things in the future. You can metally model most perl regexes as representing a tree, a tree whose job is to see if there is a path in a given string that can traverse the tree to a legal "accepting" terminal node. So in a sense using a regex to find a path toa given node is almost the opposite of what you really want.

      BTW, this is only really true of an NFA based regex engine. A DFA based regex engine would traverse all the branches of the equivelent NFA engine simultaneously, arriving at the same point, but without ever backtracking.


      ---
      demerphq

        First they ignore you, then they laugh at you, then they fight you, then you win.
        -- Gandhi


Re^2: Logic programming without using the RE engine as a crutch
by Anonymous Monk on Jul 02, 2004 at 15:20 UTC
    http://search.cpan.org/search?m=all&q=prolog&s=1&n=50 Might help some.