This sounds like a case for a full fledged database... something like DBD::RAM could come in very handy.

If I understand your specification, however, you want to get data from one column in the first file, look for matches in one column of the second file and print the whole row that contains a match.

This is why hashes were invented:

my %to_match = (); # hold things to match in here my $output = ''; # hold what we want to print # open first file while (<FILE1>) { my $element = (split(/\t/, $_))[$column1]; $to_match{$element} = 1; } # open second file while (<FILE2>) { my $element = (split(/\t/, $_))[$column2]; if (defined($to_match{$element})) { $output .= $_; } } # open output file and display results
That's very generic, and you'll probably want to change the split regex, at least. $column1 and $column2 are the 0-based numeric values of the columns you want to grab out of the specific files.

If you don't have *exact* matches, you'll have to use a regex or String::Approx or something to do a fuzzy match. This ought to get you started, though.


In reply to Re: Merging Files: A Different Twist by chromatic
in thread Merging Files: A Different Twist by Limo

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.