Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by Anonymous Monk
on Mar 13, 2007 at 14:11 UTC ( [id://604534]=note: print w/replies, xml ) Need Help??


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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Re^2: Search and delete lines based on string matching

Replies are listed 'Best First'.
Re^3: Search and delete lines based on string matching
by ptum (Priest) on Mar 13, 2007 at 14:30 UTC

    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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found