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

Christopher Nava starbuck at hawaii.rr.com
Fri Nov 7 18:31:35 CST 2003


Do you have a place I can find more info on this "flipflop" operator.
I tried google "flipflop + perl" and got nothing usefull.
I too am doing multi line file parsing (MTF messages not email) and could
use any tools you've got.

BTW: My current method is to do something like this....

open (INFILE, $filename) || die ($!);
# I've got LOTS of ram and the files are relativly short so slurping in the
entire file is no problem.
my @file = <INFILE>;
close (INFILE);

chomp @file;
#Note: I usualy use a map function to get rid of both DOS CR/LF and Unix
LFs.

my $header = 1;
my $found = "";
my %metadata = ();
my @body = ();

# You could use a while loop if you choose...
#while (<INFILE>) { # Substitute $_ for $line in the below text...

# I prefer foreach since it results in a named variable ($line) that is
easier for new persons to understand.
foreach $line (@file) {

   # Note, I try to move the most likely items toward the top to avoid
testing oddballs for every line...
    if ($header && $line =~ /^sometext$/) { # Look for the end of the header
        $header = 0;
    }elsif ($line =~ /^Subj:/) { # Multi line item
        $found = "Subject";
    }elsif ($line =~ /^Country:/) { # Single line item
        $found = "Country";
    }elseif ($line =~ /^Summary:/) { # Multi line item
        $found = "Summary";
    } #....more of same...

    # If $found is equal to an item we append it to that metadata.
    if ($header) {
        $metadata{$found} =. $line;
    } else {
        push (@body, $line);
    }
}

# Note: I'm at home and can't test the above so there are likely be bugs in
it....

-- Chris Nava

----- Original Message ----- 
From: "Jonathan Steinert" <hachi at kuiki.net>
To: "Chicago.pm chatter" <chicago-talk at mail.pm.org>
Sent: Thursday, November 06, 2003 7:10 AM
Subject: Re: [Chicago-talk] advanceing through a file w/o using an array


> 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)
>
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at mail.pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
>





More information about the Chicago-talk mailing list