And to prove that what the join utility does (see solution above) is not trivial but also not very complicated, let's try to emulate what it does in a short perl program.

perl -we 'open $_, "<", shift or die for my($D, $Q); my $p = -1e9999; +my $s = -1e9999; my $x; while (my $q = <$Q>) { chomp($q); my $k = (sp +lit " ", $q)[1]; $p <= $k or die "query not sorted"; $p = $k; while ( +$s < $p and defined(my $d = <$D>)) { (my($l), $x) = (split " ", $d)[2 +,0]; $s <= $l or die "db not sorted"; $s = $l; } my $z = $p == $s ? $ +x : "-"; print $q, " ", $z, "\n"; }' file1.db file2.query

The output is this.

1190 31277 A > T 1 0 0 - 1190 31607 C > A 0 3 1 - 1190 31629 C > T 0 2 0 - 1190 31789 A > G 1 2 5 zm1829427 1190 31882 A > C 0 4 0 - 1190 31883 T > A 0 4 0 zm445312 1190 31883 T > C 2 2 5 zm445312 1190 32199 C > T 0 1 1 - 1190 32487 T > C 0 1 1 - 1190 32496 A > G 0 3 0 -

It is an exercise to the reader to add the "Number of HITS" message.


In reply to Re: duplicates getting omitted while comparing values inside foreach. by ambrus
in thread duplicates getting omitted while comparing values inside foreach. by patric

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.