Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hmmmm. You didn't answer our question about what error you were seeing from your original code, and (based on the simplicity of the problem) I'm not entirely convinced it isn't homework. Generally, if you want help here at PerlMonks, it is better to show a little more effort, rather than just asking us to provide code. Even so, I'll help to steer you in the right direction with a few untested code snippets.

Read the contents of file A into a hash:

use strict; use warnings; my $fh; my $myfile = '/path/to/file/a'; unless (open($fh,"<",$myfile)) { die "Can't open $myfile: $!\n"; } my %delete_words = (); while (<$fh>) { chomp; $delete_words{$_}++; } close($fh);

So now you have all the words in your delete list in the hash. Next you want to open file B for reading and file C for writing (in much the same way as we opened file A) and step through the lines of file B, one at a time. Each time you have a line of file B, you want to test whether it exists in your hash. If file B contained multiple words per line, you would have to jump through more hoops, but since your file B isn't very complicated, for each line in file B you can just do something like this:

if (exists($delete_words{$_})) { # do nothing } else { # write to file C }

That's really all there is to it, except you'll want to explicitly close files B and C.


In reply to Re^3: Search and delete lines based on string matching by ptum
in thread Search and delete lines based on string matching by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (1)
As of 2024-04-19 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found