in reply to Re^2: s/// not replacing data
in thread s/// not replacing data

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.


Dave

Replies are listed 'Best First'.
Re^4: s/// not replacing data
by japhy (Canon) on Oct 31, 2005 at 16:39 UTC
    He's using
    my ($id, $badword); $sth->bind_columns(\$id, \$badword);
    to populate $badword.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

      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.


      Dave

Re^4: s/// not replacing data
by VSarkiss (Monsignor) on Oct 31, 2005 at 16:42 UTC

      Ya-all are right. I remember the old Usenet philosophy: If you post a question, you'll get an answer or two. Post an error and you'll get a whole slew of good answers. ;)


      Dave

Re^4: s/// not replacing data
by Anonymous Monk on Oct 31, 2005 at 16:41 UTC
    ####### # 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.
      Now try:
      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.
Re^4: s/// not replacing data
by Anonymous Monk on Oct 31, 2005 at 17:10 UTC
    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>); } }
      You could put your execute and bind statements inside the foreach loop, which would cause the query to be executed for each line in @keep.

      Or you could assemble an array outside the foreach loop something like:

      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.