[thelist] pattern matching mail in perl

Morbus Iff morbus at disobey.com
Fri Nov 10 07:06:36 CST 2000


>Even a line at a time ( If I could somehow do a while loop and grab and test
>each line as it reads the file might work) might be to much.  Perhaps if I
>could grab each individual word and test it against my search word.
>
>And there in lays the problem.  I know what I need to be doing but don't
>know how to do it.  My reference talks about pattern matching but does not
>give an example that I can play with.  I've searched online resources for
>examples but found nothing.

open (MAIL, "$file"); $file = <MAIL>; close (MAIL);

foreach (split(/\n/, $file)) {
   if ($_ =~ /searchterm/) { print "Found it: $_"; }
}

This is a really simple one. The first line obviously sucks in your file,
and we place it into a big $variable instead of an array. We then loop
through that variable in our second line, splitting on a new line character.

So, the loop will go through every single line in the file, and do anything
in the loop on that one line. We use perl's $_, which contains the latest
of what we're working on (in this case, the single line we just yanked out
from our $file).

We then do a test to see if that line matches our "searchterm". If it does,
we print that line out, and move on with our loop. The pattern match can be
anything obviously, some examples being:

   /Invoice Number:/
   /Subject: /
   /From: bob\@microsoft.com/             # we must escape "@" here

And so on. Hope that helps.

-- 
Morbus Iff
   ______       Here we have HomelessMorbus - living in a box. Able to
   | () |       furnish an old refrigerator box into a spacious 3 room
   |<\/>|      complete with Internet connection and entertainment center,
   |_/\_|       until it rains, and then he's up to his knees in shat...
                   Devil Shat: <http://www.disobey.com/devilshat/>

-01--- <\/> ---- <http://www.disobey.com/> --- Bad Ascii, Short Notice ----





More information about the thelist mailing list