#!/usr/bin/perl use strict; use warnings; ... while (<$book>) { my $line = $_; my $plainline = $line; $plainline =~ s/["'.,!?:;\-()[\]{}|\\\/]/ /g; #replace all punctuation with a space my @sentence = split(/ /,$plainline); foreach my $word (@sentence) { chomp($word); my $whichword = 0; # to track which badword was found foreach my $badword (@badwords) { if (lc($word) eq $badword) { my $newword = replaceword($whichword); #get a cleaner word to replace the naughty word $line =~ s/($word)/$newword/i; } $whichword++; } } $cleanbook .= $line; }