Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Blocking based on words in a list

by moot (Chaplain)
on Dec 28, 2005 at 00:50 UTC ( [id://519470]=note: print w/replies, xml ) Need Help??


in reply to Blocking based on words in a list

chomp for @blocked; # don't expect to match newlines if ( grep { $FORM{'comments'} =~ /\b$_\b/ } @blocked) { ... }
This is somewhat simplistic, not to mention inefficient - the grep continues over every word in the list even if the first word matches. A better approach would use a for loop:
chomp for @blocked; for my $m (@blocked) { if ($FORM{'comments'} =~ /\b$m\b/) { # print your message here. exit; # could also last outside the loop } }
The \b markers are to match at a word boundary, so BLOCK will not be matched in THIS SHOULD BE UNBLOCKED, for example.

However you may want to re-think your approach. Simplistic blockers like this rarely achieve the desired results, and often produce too high a level of false positives to be truly useful. YMMV, of course.

Replies are listed 'Best First'.
Re^2: Blocking based on words in a list
by VSarkiss (Monsignor) on Dec 28, 2005 at 02:28 UTC
Re^2: Blocking based on words in a list
by strat (Canon) on Dec 28, 2005 at 08:35 UTC
    $FORM{'comments'} =~ /\b$_\b/ }

    That may give errors or unexpected behaviour if there are metachars for REs in @blocked (e.g. opening parantheses, dots, ...). Either use quotemeta for each $_, or \Q$_\E, e.g.

    @blocks = map { quotemeta($_) } @blocks; ... $FORM{'comments'} =~ /\b$_\b/
    $FORM{'comments'} =~ /\b\Q$_\E\b/

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://519470]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-19 15:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found