You should store the pairs in a hash.

You are not very specific about what to do when multiple values are present, here is an implementation that visits all siblings that can be visited from one parent (you are representing a tree, right?)

#!/usr/bin/perl use strict; use warnings; my %chain; # read the chain: while (<DATA>){ chomp; my ($key, $val) = split m/ \| /; push @{$chain{$key}}, $val; } # walk the chain starting from 12: walk(12); sub walk { my $c = shift; print "Visiting $c\n"; for (@{$chain{$c}}){ walk($_); } } __DATA__ 23 | 32 12 | 45 12 | 35 12 | 67 45 | 34 34 | 90 34 | 55 35 | 53 35 | 44 44 | 41

And the corresponding output:

Visiting 12 Visiting 45 Visiting 34 Visiting 90 Visiting 55 Visiting 35 Visiting 53 Visiting 44 Visiting 41 Visiting 67

Update: If you have cycles in your chain this will loop forever. If you do, you have to mark the pairs that you have used in the traversal, and never use a pair twice.


In reply to Re: research the search by moritz
in thread research the search by baxy77bax

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.