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

The fetch is to retrieve the swearwords from the database. It does it because I can print out the swearwords.

The process is like this:

1) All the chat info (name, message, ip, time) is retrieved from the DB and placed into an array @keep.
2) The list of my swear words are fetched and applied to the data above. This is pushed into the new array @keep_after_swear_words.
3) The emoticon filters then optimizes the swear-filtered data and applies it's last filter
4) The text is then printed to screen.

Replies are listed 'Best First'.
Re^3: s/// not replacing data
by VSarkiss (Monsignor) on Oct 31, 2005 at 16:38 UTC
      If this is the problem, would it not be easier for them to store the badwords into an array and apply it to each line?


      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
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.


    Dave

      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

        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

      ####### # 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.
      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.