http://www.perlmonks.org?node_id=249478

ACJavascript has asked for the wisdom of the Perl Monks concerning the following question:

HEY AGAIN! hehe Heres another stupid question by me hehe :D

I am trying to have an array of Words seperated by commas " , " -- Now when the user types in some words if they say a swear the scripts looks through the array and puts a **** in the words place.. Now I can get it to do it but for some reason it only works for the first word in the array.. So PLEASE HELP Hehe...

open(info,"thedatabase.db"); @bb=<info>; close(info); foreach $l(@bb){ $comment = s/$l/****/g; }


Thats pretty much it, Any help is much appreciated!
Thanks!!

Replies are listed 'Best First'.
Re: Array Trouble!
by The Mad Hatter (Priest) on Apr 10, 2003 at 00:29 UTC
    Well for one, correct your syntax. The fifth line should be something like
    $comment =~ s/$l/****/g;
    = isn't the right operator for substitution.

    Here is some working code that you should be able to modify.

    my $foo = "man, that's some badass motherfucking shit!"; my @badwords = qw/ass fuck shit/; foreach my $word (@badwords) { my $bleep = "*" x length($word); $foo =~ s/$word/$bleep/g; } print $foo, "\n";
    which outputs
    man, that's some bad*** mother****ing ****!

    Updated Formatting

    Update 2 In your database, the words are one per line it seems. Therefore, the words all have a \n on the end of them, so unless the "bad" word in the comment has a newline after it, it won't match. Make the syntax correction I noted above, and add chomp($l); as the first statement in your loop, and it should work. The chomp removes the last character of a string if it is a newline.

      Yea the = was a miss print on my part,,,
      Hey both of yours worked GREAT!
      THANK YOU BOTH SO VERY MUCH!!
Re: Array Trouble!
by dvergin (Monsignor) on Apr 10, 2003 at 21:37 UTC
    It would be better not to use '$l' (dollar-ell) as a variable name -- especially in the neighborhood of a regex. It looks too much like '$1' (dollar-one) which has special use in regexes with capturing parentheses.
A reply falls below the community's threshold of quality. You may see it by logging in.