Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re^3: Search and delete lines based on string matching

by ptum (Priest)
on Mar 13, 2007 at 14:30 UTC ( #604541=note: print w/ replies, xml ) Need Help??

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.

Comment on Re^3: Search and delete lines based on string matching
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://604541]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2015-11-27 07:29 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (719 votes), past polls