CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Delete Certain Elements From Array

 



perlkid
stranger

May 29, 2000, 12:43 PM

Post #1 of 7 (1142 views)
Delete Certain Elements From Array Can't Post

 
Hi guys,

I'm trying to write a filter that takes out all elements containing dirty words.

Right now I have a long list of dirty words like so,

if (($results =~ /dirty/i) | | ($results =~ /dirty/i))
{
next;
}

This goes on to about 35 words.

$results is the html with the title description in it. This method doesn't work well at all.

So I would like to just take them out of the array ahead of time before they are even delt with.

I tried this,

foreach $filter (@results)
if (($filter =~ /dirty/i) | | ($filter =~ /dirty/i))
{
shift(@results);
}

But That doesn't work because it's taking them of the left. They could be results that have nothing to do with porn but they would be taken off, .. I think.

Please Help,

Thank You,

plerkid


japhy
Enthusiast

May 29, 2000, 1:03 PM

Post #2 of 7 (1142 views)
Re: Delete Certain Elements From Array [In reply to] Can't Post

Here's a pretty complete solution.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# an array of our bad words
@badwords = (
"BAD", "NAUGHTY", "NASTY", "TABOO",
"UNGOOD", "EVIL", "ICKY",
);

# can also do:
@badwords = qw(
BAD NAUGHTY NASTY TABOO UNGOOD EVIL ICKY
);

# construct the bad-word regex
$bad_REx = join "|", map quotemeta, @badwords;

# filter out the bad stuff
@safe_data = grep !/$bad_REx/io, @data;
# the /i is for case-insensitive matching
# the /o is "compile this regex once"
</pre><HR></BLOCKQUOTE>


perlkid
stranger

May 29, 2000, 2:02 PM

Post #3 of 7 (1142 views)
Re: Delete Certain Elements From Array [In reply to] Can't Post

 
Thank You Japhy,

That worked.

You have saved me at least 12 hours of frustration. Smile Smile Smile

Thanks,

Perlkid


dws
Deleted

May 30, 2000, 9:55 PM

Post #4 of 7 (1142 views)
Re: Delete Certain Elements From Array [In reply to] Can't Post

This approach will give false positives (e.g., rejecting "sextant" because it contains "sex"). Consider using \b (word break) in the regular expression.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$bad_REx = join "|", map { "\b" . quotemeta($_) . "\b" } @badwords;</pre><HR></BLOCKQUOTE>



perlkid
stranger

May 31, 2000, 8:13 AM

Post #5 of 7 (1142 views)
Re: Delete Certain Elements From Array [In reply to] Can't Post

 
Thanks For The Tip. I'll see if I can incorperate that. Thanks.

perlkid


japhy
Enthusiast

May 31, 2000, 8:31 AM

Post #6 of 7 (1142 views)
Re: Delete Certain Elements From Array [In reply to] Can't Post

dws - yes, I'm aware of that. I used the phrase "pretty complete", and left the eventuality of words containing "bad words" (like class or sextant) up to perlkid to learn about.


dws
Deleted

May 31, 2000, 9:11 AM

Post #7 of 7 (1142 views)
Re: Delete Certain Elements From Array [In reply to] Can't Post

Oops. Trampled on another learning opportunity.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives