1825801 Members
2223 Online
109687 Solutions
New Discussion

Perl question

 
Jeffrey S. Sims
Trusted Contributor

Perl question

Alright I give up. I seriously need some help with this perl script.

Firswt let me tell you what I want it to do. I have procmail rules that filter incoming emails based on keywords (commingly used in spam and porn spam) and it filters it to a mailbox which I then go and check. What I want is to look at the mailbox with this script and print the keyword that matched as well as the line in the email that matched. So far this is what I have:

#!/usr/bin/perl


open( WORDLIST, "dirtywordlist.lst") or die( "Couldn't open word list: $!");
@list = ;
close( WORDLIST ) or die( "Couldn't close word list: $!");

open( EMAILFILE, "dirtytest") or die( "Couldn't open word list: $!");
@email = ;
close( EMAILFILE ) or die( "Couldn't close word list: $!");

foreach( @list){
$matched = grep /$_/i, @email;

if( $matched ){
print "$_\n";
print "$matched\n\n";
}
}

I am fairly new to perl and would greatly appreciate any help that someone can lend.

dirtywordlist.lst is the list of keywords to grep for and dirtytest is a test message I greated to see if the script is working correctly.

When I run this it print every keyword for $_ and $matched prints as 3 for every keyword. The file only contains one word that should match.

Thanks in advance for your help.
1 REPLY 1
H.Merijn Brand (procura
Honored Contributor

Re: Perl question

Where's your chomp's? :) You are now matching *with* newlines. And for optimization of the process, use qr//

#!/usr/bin/perl

sub file2list ($)
{
my $file = shift;
open FILE, "< $file" or die "Couldn't open $file: $!");
chomp (@list = );
close LIST or die "Couldn't close $file: $!");
@list;
} # sub2list

my @list = map { qr/$_/i } file2list ("dirtywordlist.lst");
my @email = file2list ("dirtytest");

foreach (@list) {
my $matched = grep /$_/, @email or next;
print "$_\n";
print "$matched\n\n";
}
Enjoy, Have FUN! H.Merijn