Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
If the files can be stored in sorted order (or you can maintain an index on them that lets you access them in sorted order quickly a-la b-tree or you don't mind going through the overhead of sorting both before performing the comparison) based on the fields you want to compare then you could step through the 2 of them in lock-step fashion basically like the merge step of the mergesort algorithm. Pseudoperlcode (based on the assumption that the entire line is the key you want to match):

open FILE1,"<file1"; open FILE2,"<file2"; my $key1=<FILE1>; my $key2=<FILE2>; while((!eof(FILE1)&&(!eof(FILE2))){ if($key1 gt $key2){ # do something when you find a key in FILE2 and not in FILE1 # read a line from FILE2 $key2=<FILE2> }elsif($key1 lt $key2){ # do something when you find a key in FILE1 and not in FILE2 # read a line from FILE1 $key1=<FILE1>; }else{ #found a match, read a line from FILE1 and FILE2 #this behavior may vary depending on how you want to #handle multiple matches, i.e. a given line is in both #files more than once $key1=<FILE1>; $key2=<FILE2>; } } close FILE1; close FILE2;

That way doing the check for common records is as fast as reading each file once and you never have to hold more than one record from each file in memory at a time.

L


In reply to Re: Efficient search through a huge dataset by lhoward
in thread Efficient search through a huge dataset by johnnywang

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (3)
    As of 2020-12-04 02:15 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How often do you use taint mode?





      Results (58 votes). Check out past polls.

      Notices?