http://qs321.pair.com?node_id=604541


in reply to Re^2: Search and delete lines based on string matching
in thread Search and delete lines based on string matching

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.