If my task description were, "Read a text file, compare it to a banned word list, and then rewrite the text file, minus the banned words." I would probably do it something like this. ...note, this isn't a cut-and-paste solution for you, it's just an example from which you hopefully can derive a solution taylored to your needs...
use strict; # Please do this! use warnings; # And this! # First, read from the <DATA> filehandle to grab the list # of banned words. map the banned words into hash-keys. my %banned = map { chomp $_; $_=> undef } split /\s+/, <DATA>; my $infile = "textfile.txt"; my $tmpfile = "outfile.tmp"; open my $in, "<$infile" or die "Cannot open $infile.\n$!"; open my $out, ">$tmpfile" or die "Cannot open temp output file.\n$!"; while ( my $inline = <$in> ) { chomp $inline; my $printline = ""; foreach my $word ( split /\s+/, $inline ) { next if exists $banned{ lc $word }; $printline .= $word; } print $out "$printline\n"; } close $out; close $in; rename $tmpfile, $infile; __DATA__ a at be for and to of in the as i it are is am on an you me b c d e f g h j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 10
I hope this helps... good luck.
Dave
In reply to Re: Removing common words
by davido
in thread Removing common words
by mkurtis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |