Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

You basically want to calculate the intersection of two lists (and the negation of that). The following algorithm will work, assuming relatively small lists and unsorted initial input:

  1. Verify your commandline arguments (did the user specify two files, and do they exist?)
  2. Open dellist, and for each line:
    1. split/parse the line as necessary (I'm not sure what you actually want to key on... just the ID, or the whole mess?)
    2. Put the key into a %del hash, with a value of 1.
  3. Close dellist for good.
  4. Open orglist. For each line:
    1. Parse again as per 2(1), above. (Say, maybe you could write a function to re-use for that.)
    2. If the key exists in %del, you have a match; save to beepon, else save to beepoff

The more general algorithm, above, runs at near-linear time but will consume memory proportional to the number of elements in your DEL list (which is still much better than your solution, which re-reads the entire DEL list for each line of the ORG list!)

Less general option: If your initial files are known to be pre-sorted by your key value, you can traverse each with a pair of list cursors. The advantage of this approach is that it runs in strict linear time on the total number of elements across both lists, and uses a small constant amount of memory (i.e., memory does not increase with the size of the DEL list.)

You might also want to look at List::Compare (specifically, the intersection method) for a reusable solution if your data is small enough where storing both sets in RAM isn't an issue.

Have fun!


In reply to Re: Search and Remove by rjt
in thread Search and Remove by PyrexKidd

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!
  • 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 making s'mores by the fire in the courtyard of the Monastery: (2)
    As of 2015-07-05 03:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (60 votes), past polls