Somebody mentioned encoding the strings of characters into strings of bits, but I am not sure how to do that and how fast it would be?

If your strings are upto 16 characters in length, then bit-encoding the characters will result in a 32-bit unsigned integer:

my %acgt; @acgt{ qw[ a c g t ] } = 0 .. 3; sub encode { my $string = lc shift; die "String '$string' >16 chars" if length $string > 16; my $bits = ''; vec( $string, $_, 2 ) = $acgt{ substr $string . 'a' x 16, $_, 1 } for 0 .. 15; return unpack 'N', $bits; }

You can then build a single bit-vector to represent the entire search space using 1-bit per possible string in ram (512MB). Set the bits corresponding to the contents of your smaller file:

my $lookup = ''; while( <SMALLFILE> ) { chomp; vec( $lookup, encode( $_), 1 ) = 1; }

and then processing the larger file becomes an O(1) lookup:

while( <BIGFILE> ) { chomp; print if vec( $lookup, encode( $_ ), 1 ); }

No sorting, searching, hashing or DBs. Just simple code and very fast lookup. If your strings are 16 characters or less.


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: Comparing strings (exact matches) in LARGE numbers FAST by BrowserUk
in thread Comparing strings (exact matches) in LARGE numbers FAST by perlSD

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.