in reply to Substitute 'bad words' with 'good words' according to lists
It is not the most efficient way, as you are going through the entire sentence for each key work. Say you have n key word, and the sentence contains m words, you are checking n*m times.
One of the better way is to split the sentence into words, go through the list, and see whether each word exists in the hash (hash search is nothing). This only requires to go through the sentence three times: 1) once to split. 2) once to replace, and 3) if you wish to count this one, to join it back.
use strict; use warnings; my %words = ( ugly => 'ug**', anotherugly => 'anot*******', ); my $txt = "ugly anotherugly"; my @words = split / /, $txt; # largely simplified, you have to count , +.:; etc for my $i (0 .. $#words) { $words[$i] = $words{$words[$i]} if (exists($words{$words[$i]})) } print join(' ', @words);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Substitute 'bad words' with 'good words' according to lists
by sk (Curate) on Sep 26, 2005 at 03:00 UTC | |
by pg (Canon) on Sep 26, 2005 at 05:14 UTC |