There is nothing wrong with your code. You are extracting all words and matching all badwords. I slightly adapted your code and added some print-statements to see what happens:
use strict; use warnings; my @badwords = qw/crud crap poo wanker/; while (<DATA>) { my $line = $_; my $plainline = $line; $plainline =~ s/["'.,!?:;\-()[\]{}|\\\/]/ /g; #replace all punctua +tion with a space my @sentence = split(/ /,$plainline); print join '|', @sentence,"\n"; foreach my $word (@sentence) { chomp($word); print "\tChecking *$word*\n"; my $whichword = 0; # to track which badword was found foreach my $badword (@badwords) { if (lc($word) eq $badword) { print "\t\tFound $badword\n"; #my $newword = replaceword($whichword); #get a cleaner + word to replace the naughty word #$line =~ s/($word)/$newword/i; } $whichword++; } } #$cleanbook .= $line; } __DATA__ "Wanker!" said I. "Crap," says Travis. This is horse poo. Poo, I tell you! "Poo upon all the crud and crap the wanker could see."

And the output of this is:

|Wanker|||said|I| | Checking ** Checking *Wanker* Found wanker Checking ** Checking ** Checking *said* Checking *I* Checking ** |Crap|||says|Travis| | Checking ** Checking *Crap* Found crap Checking ** Checking ** Checking *says* Checking *Travis* Checking ** This|is|horse|poo||Poo||I|tell|you| | Checking *This* Checking *is* Checking *horse* Checking *poo* Found poo Checking ** Checking *Poo* Found poo Checking ** Checking *I* Checking *tell* Checking *you* Checking ** |Poo|upon|all|the|crud|and|crap|the|wanker|could|see|| | Checking ** Checking *Poo* Found poo Checking *upon* Checking *all* Checking *the* Checking *crud* Found crud Checking *and* Checking *crap* Found crap Checking *the* Checking *wanker* Found wanker Checking *could* Checking *see* Checking ** Checking **
Maybe the error is in your replaceword sub?

As you see, you have lots of empty items in your wordslist since you replace punctuation by spaces and then split on spaces, effectively dropping all spaces but making a lot of empty "words".

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law


In reply to Re: Match first word in line fails by CountZero
in thread Match first word in line fails by yacoubean

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.