Amer Neely wrote:
> Xicheng Jia wrote:
> > Amer Neely wrote:
> >> This is driving me nuts.
> >>
> >> I'm walking through a mailbox file, and want to pull out specific lines
> >> from each message. The body of each message is in a similar format,
> >> having been generated by a script.
> >>
> >> I'm doing OK except for one particular block of lines, the customer
> >> address data. There is a blank line before and after this block. Example:
> >>
> >> Transaction Time: 18:45:55
> >>
> >> Amer Neely
> >> POB 1481 Station Main
> >> North Bay ON
> >> P1B 8K7
> >> CANADA
> >>
> >> 123-456-7890
> >>
> >> I've managed to get the 5 lines into a string using this code:
> >>
> >> while <IN>
> >> {
> >>
> >> # bunch of other comparisons deleted
> >>
> >> if (/^Transaction Time:/ ... /^\d\d\d-\d\d\d-\d\d\d\d$/)
> >
> > by using /A/ ... /B/ expression, you are still in single-line mode, if
> > you want to get all these lines in $_, and then parse the data, try to
> > reset the IRS $/ to something like:
> >
> > local $/ = "Transaction Time:";
> >
> > then you can use block-mode which seperates your records by the given
> > string "Transaction Time:" in $/,
> >
> > Xicheng
> >
>
> Thanks for the quick reply. Still a little foggy though.
> If I set the record separator to "Transaction Time:", then I don't need
> the 'if (/^Transaction Time:/ ... /^\d\d\d-\d\d\d-\d\d\d\d$/)' loop?
yes, you dont need this "if" loop coz it invokes perl in line-mode by
default (in fact it depends on yout $/)..
>
> Then set $CustData = $_ ?
>
> But doesn't that leave me in the same position? All 5 lines are now in
> $CustData.
not really, after you do so, you get something like:
$_ = "18:45:55
Amer Neely
POB 1481 Station Main
North Bay ON
P1B 8K7
CANADA
123-456-7890
"
then split it with "\n" like: my @arr = split "\n";
you get:
$arr[0] = "18:45:55";
$arr[1] = "";
$arr[2] = "Amer Neely";
$arr[3] = "POB 1481 Station Main";
$arr[4] = "North Bay ON"
........
so you use the following line to collect your date..:
my (undef, undef, $var1, $var2, $var3, $var4, $var5, undef, undef) =
split "\n";
or you can use regex to parse whatever data you need from $_. it really
depends on what information do you really need.
Another way: if you are sure there are 5 lines for each record you want
to keep, then you can read your data in paragraph-mode,like:
local $/ = "";
while ( <IN> ) {
next unless tr/\n// > 5; #use paragraph only have more than 5
lines(count also a blank line, so you have 6 lines)
my ($name, $pob, $add1, $add2, $cont) = split "\n";
# do sth on the avobe variables..
}
then you get:
-------------------------
$name = "Amer Neely"
$pob = "POB 1481 Station Main"
$add1 = "North Bay ON"
$add2 = "P1B 8K7"
$cont = "CANADA"
------------------------
Xicheng
> >> {
> >> $CustData = $_;
> >> $CustData =~ s/^Transaction Time:.+//; # lose the beginning pattern
> >> $CustData =~ s/^\d\d\d-\d\d\d-\d\d\d\d$//; # lose the ending pattern
> >> next if ($CustData =~ m/^$/); # skip the blank lines
> >> $CustData =~ s/\n//g; # get rid of blank lines. don't think this working
> >> print "\t$CustData\n";
> >> }
> >> }
> >> close IN;
> >> print "\nAll done.\n";
> >>
> >> The problem seems to be that $CustData holds all 5 lines. I need to
> >> break out each of the lines into a separate string variable so as to
> >> populate a database field. This is what has me stumped. Sure would
> >> appreciate some light on this.
> >>
> >> --
> >> Amer Neely
> >
>
>
> --
> Amer Neely
> Home of Spam Catcher
> W: www.softouch.on.ca
> E:
> Perl | MySQL | CGI programming for all data entry forms.
> "We make web sites work!"