Since the numbers in the left hand column are unique and are the keys to your searches, you could load the data file into a hash instead of an array. Your answer then comes from a direct hash lookup rather than having to search.

#!/usr/bin/perl -w use strict; my %kids=map { split } <DATA>; print "Father of $_ is $kids{$_}\n" for @ARGV; __DATA__ 1045316394 1045316144 1045316407 1045316394 1045316419 1045316407 1045316438 1045316419 1045316469 1045316394 1045316492 1045316407 1045316505 1045316492

Example usage:

$ perl fatherson.pl 1045316419 1045316492 Father of 1045316419 is 1045316407 Father of 1045316492 is 1045316407

Of course, you should add some code to do something sensible if the requested child is not found.

Update: You want the full lineage. You could do something like this.

sub print_lineage { my ($x) = @_; my @p; while (exists $kids{$x}) { $x = $kids{$x}; push @p,$x; } if (@p) { local ($") = ", "; print "Lineage of $_[0] is @p\n"; } else { print "Lineage of $_[0] is unknown\n"; } } print_lineage $_ for @ARGV;

Which produces:

$ perl fatherson.pl 1045316419 1045316469 Lineage of 1045316419 is 1045316407, 1045316394, 1045316144 Lineage of 1045316469 is 1045316394, 1045316144
--- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';

In reply to Re: Database problem. by pfaut
in thread Correction to Database problem by nasa

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.