[Chicago-talk] advanceing through a file w/o using an array

Jonathan Steinert hachi at kuiki.net
Thu Nov 6 11:10:14 CST 2003


Dooley, Michael wrote:
> ok, I haven't looked at perl in well pretty much forever.
> 
> #! /usr/bin/perl -w
> 
> $FILE='path_to_mail_file';
> 
> open (MAILFILE, "$FILE") || die "Can not read file ($!)";
> 
> while (<MAILFILE>) {
>         chomp;
>         if ($_ =~ /^From / || /^Subject: /) {
>                 print "$_\n";
>         }
> }
> 
> 
> ok this is all I have so far. which is pretty easy in the first place.
> the part I am working on is getting the body of the email and displaying
> that.
> 
> the body comes after "Message-Id: " and ends on the line befor "^From ".
> 
> Is there a variable I can use to print the line after a match untill another
> match is found?
> 
> kinda like 
> if ($_ =~ /Message-Id: /) {
>                 print the next line until $_= "^From "
>         }

Actually, there is an operator for this sort of thing '..' is the 
flipflop operator. The only trouble is that it's inclusive when you use 
it like this:

if (/^Message-Id: / .. /^From: /)

so in that case you would get both the Message-Id and From lines, which 
is not what you want. Basically, turning it around so the inclusive 
matches point the other way is closer to what you want in this case.

unless (/^From: / .. /^Message-Id: /)

Of course, the cleanest way to parse real mail files (what this looks 
like) is to use a module for that purpose.

--Jonathan (hachi)




More information about the Chicago-talk mailing list