Which works really fast, but then I don't want to load a million lines into memory at once.

Why not?

As the "keys" for matching are numeric, you can save some space by using a bitstring for your lookup:

use autodie qw(open close); open my $fip, '<', 'file-1'; open my $fop, '<', 'file-2'; my $lookup = ''; while (my $line = <$fop>) { chomp $line; vec( $lookup, $line, 1 } = 1; } close $fop; while (my $line = <$fip>) { my @token = split(/-/, $line, 2); if ( vec( $lookup, $token[0], 1 } ) { print $line; } } close $fip;

For a range upto 1 million, that will use <1/4MB for the lookup, rather than close to 100MB and should be at least as fast.

Update: Actually it is ~10x faster than the hash lookup, though you probably won't notice the difference as you will be limited by the time taken to read the second file from disk. But in any case, it will be 2 or 3 orders of magnitude faster than any DB solution.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: Matching data between huge files by BrowserUk
in thread Matching data between huge files by est

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.