This is completely untested:

# Call as path( $workstation, $server ) sub path { my ( $workstation, $server, @start ) = @_; # initial invocation; make the ws the whole of the path if ( !@start ) { return path( $workstation, $server, $workstation ); } my $last_hop = $start[ -1 ]; # found the server; we're done! if ( $last_hop eq $server ) { return @start; } # for every possible next hop, NEXT_HOP: foreach my $next_hop ( get_next_hops( $last_hop ) ) { # if it's already in our path somewhere, skip it. foreach my $previous_hop ( @start ) { next NEXT_HOP if ( $previous_hop eq $next_hop ); } # look for a path with this as the next hop my @out = path( $workstation, $server, @start, $next_hop ); # if a path was found, return it return @out if @out; } # dead end return (); }

There's probably a module to do this better, but I don't know what it is. The above may not be as efficient as an iterative version could be. It makes no attempt to find the shortest path. get_next_hops is left to you (it has to return a list).


In reply to Re: Finding a Path by kyle
in thread Develop a Tree by Herkum

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.