in reply to Match first word in line fails

Here's another way to approach the problem. I'm not sure it's a better way than holli layed out for you but if you've got a bunch of words to replace I'd think a hash might be the way to go. Unfortunately this doesn't save your puncuation, and tonight I'm at a loss on how to split and save puncuation.

With that said, here's my attempt:

#!/usr/bin/perl use strict; use warnings; my ($line, $cleanbook); my %badwords = (crud => "darn", shit => "shoot"); while ($line = <DATA>) { $line =~ s/["'.,!?:;\-()[\]{}|\\\/]/ /g; #replace all punctuation +with a space my @sentence = split(/\W+/,$line); @sentence = map {$badwords{lc($_)}?$badwords{lc($_)}:$_} @sentence +; $cleanbook .= join(' ', @sentence)."\n"; } print $cleanbook; __DATA__ "Crud," said Travis "Shit, this crud is shit!"
Useless trivia: In the 2004 Las Vegas phone book there are approximately 28 pages of ads for massage, but almost 200 for lawyers.

Replies are listed 'Best First'.
Re^2: Match first word in line fails
by Anonymous Monk on Jan 31, 2005 at 17:47 UTC

    Change this:

    my @sentence = split(/\W+/,$line);

    to:

    my @sentence = split(/(\W+)/,$line);

    Now @sentence will contain the punctuation as well.