Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Array Trouble!

by ACJavascript (Acolyte)
on Apr 09, 2003 at 23:49 UTC ( [id://249478]=perlquestion: print w/replies, xml ) Need Help??

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!

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://249478]
Approved by diotalevi
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-07-24 08:31 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.