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);
In reply to Re: Substitute 'bad words' with 'good words' according to lists
by pg
in thread Substitute 'bad words' with 'good words' according to lists
by mulander
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |