Froskoy
Froskoy

Reputation: 127

Perl - regexp not being replaced

I have an array containing words that I want to remove from each line of a file. The code I am using is as follows:

my $INFILE;
my $OUTFILE;
my $STOPLIST;
open($INFILE, '<', $ARGV[0]);
open($STOPLIST, '<', "stop.txt");
open($OUTFILE, '>', $ARGV[1]);

my @stoplist = <$STOPLIST>;

my $line;
my $stopword;
while (<$INFILE>) {
    $line = $_;
    $line =~ s/\[[0-9]*\] //g;
    $line =~ s/i\/.*\/; //g;
    foreach (@stoplist) {
        $stopword = $_;
        $line =~ s/${stopword}//g;
    }
    print $OUTFILE lc($line);
}

However, the words in the stoplist still appear in the text in the output file, which would indicate that the $line =~ s/${stopword}//g; line wasn't doing it's job as I expected.

How can I make sure that all words in the stop list that appear in the input text are replaced with 0 characters in the output?

Upvotes: 1

Views: 63

Answers (1)

toolic
toolic

Reputation: 62037

You need to remove newlines from your stoplist using chomp:

my @stoplist = <$STOPLIST>;
chomp @stoplist;

Upvotes: 2

Related Questions