Re^3: s/// not replacing data
by VSarkiss (Monsignor) on Oct 31, 2005 at 16:38 UTC
|
That's not what you're doing. You run thru your list of swear words only once, applying it to the first line in @keep. If you intend to apply every substitution to every chat line, you need to either re-fetch the data, or, if there's not that many of them, keep them in memory.
| [reply] [d/l] |
|
|
| [reply] |
|
|
Isn't that what I just wrote? :-)
I think the terminology of whether badword/swearword is the pattern or the target is causing confusion. The OP has all the target strings in memory, but is fetching the patterns and substitutions once, only against the first string. That may indicate that there are more than will fit in memory. If not, then yes, it's much easier if everything's in arrays.
| [reply] |
|
|
Re^3: s/// not replacing data
by davido (Cardinal) on Oct 31, 2005 at 16:34 UTC
|
Yes, but how are you assigning the results of $sth->fetch() to $badword? That seems to be missing. Perhaps $badword is never being populated. You can test this by putting a print "$badword\n"; into your while loop.
| [reply] [d/l] [select] |
|
|
my ($id, $badword);
$sth->bind_columns(\$id, \$badword);
to populate $badword.
| [reply] [d/l] |
|
|
Blind as a bat, I must be. Thanks japhy. I even looked for binding and didn't spot it. By the way, regarding blind bats, I had a chance to venture into a bat cave a month or so ago. When I took a picture with flash, the picture shows thousands of eyes glowing from the reflection of the flash as they stare back at me.
| [reply] |
|
|
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
#######
# connecting to the DB again so we can filter swear words
#######
my $data = qq(SELECT id, word FROM swears);
my $sth = $dbh->prepare($data);
$sth->execute() or die $dbh->errstr;
my ($id, $badword);
$sth->bind_columns(\$id, \$badword);
foreach my $line (reverse @keep)
{
my ($username, $message, $date, $ip) = split(/<!!>/, $line);
#print "user: $username<br> message: $message<br> date: $date
+<br> ip: $ip<br><br>";
while ($sth->fetch)
{
$username =~ s/\b$badword\b/ **** /gi;
$message =~ s/\b$badword\b/ **** /gi;
print "the badword was: $badword<br>";
}
print "message: $message<br>";
push(@keep_after_swear_words, "$username<!!>$message<!!>$date<!!
+>$ip");
}
Does print out the swearword each time through the loop. I added the test print which I used originally. It's getting the data from the bind'd column. | [reply] [d/l] |
|
|
while ($sth->fetch)
{
$username =~ s/\b\Q$badword\b/ **** /gi
or print "[$badword] not found in $username\n";
$message =~ s/\b\Q$badword\b/ **** /gi
or print "[$badword] not found in $message\n";
}
The \Q is mostly precautionary, in case $badword might have a regex-special char in it.
Caution: Contents may have been coded under pressure.
| [reply] [d/l] |
|
|
Hi guys. Sorry.
I got the swearwords working with using a suggestion to make an array of badwords and then apply the s///.
That worksgreat now. I don't know how to apply that same fix or idea for the emoticon filtering beacause it's more than one replacement.
For emotions there is a face (contains something like: *smile*), a location (containing the URL of the image) and a name (something like: smilie). So it's a three-part s///. How could this be done using the array method because this, too, is having the same problem as the swearwords did.
Thanks!
#######
# connecting to the DB again so we can filter emoticons
#######
my $data = qq(SELECT id, name, location, face FROM emoticons);
my $sth = $dbh->prepare($data);
$sth->execute() or die $dbh->errstr;
my ($id, $name, $location, $face);
$sth->bind_columns(\$id, \$name, \$location, \$face);
foreach my $line (@keep)
{
my ($username, $message, $date, $ip) = split(/<!!>/, $line);
while ($sth->fetch)
{
$message =~ s|(?<!\S)\Q$face\E(?!\S)| <img src="$location" alt="
+$name"> |gi;
}
if ($message =~ m|^/me|i)
{
$message =~ s|^/me||i;
print qq(<b><i><a href="#" TITLE="Message sent on $date by $ip">
+$username</a></i></b> <i>$message</i><br>);
}
else
{
print qq(<b><a href="#" TITLE="Message sent on $date by $ip">$us
+ername</a>:</b> $message<br>);
}
}
| [reply] [d/l] |
|
|
my @emoticons;
push @emoticons, [$id, $name, $location, $face] while $sth->fetch;
Then inside your foreach, instead of the while/fetch, you'd have something like:
foreach my $emote_aref (@emoticons) {
($id, $name, $location, $face) = @$emote_aref;
...
Caution: Contents may have been coded under pressure.
| [reply] [d/l] [select] |