One thing to do would be to test for Tide in the line(Assuming all tide data
ends with the word "Tide") right away...then do special stuff for each case
in an if:

if( /Tide$/ ) # If last word in line is Tide....
{
}
else # Must be lunar
{
}

And just a note, if you're just going to put the date and time fields back
together, don't seperate them in your pattern match.

($date, $time, ... ) = ^(\d+-\d+-\d+)\s+(\d+:\d+)...$/

----- Original Message -----
From: "Jack Lauman" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, June 21, 2001 3:48 PM
Subject: Another Regex Question


> I'm trying to create a CSV file from the text data below.  Lines
> containing High and Low Tide data have 9 fields, lines having
> sunrise/sunset and lunar data have 8 fields.
>
> How you differentiate between the two conditions?
>
> 2000-12-03 11:30 AM PST   9.39 feet  High Tide
> 2000-12-03  4:15 PM PST   Sunset
> 2000-12-03  7:56 PM PST   First Quarter
> 2000-12-04  3:42 AM PST   2.81 feet  Low Tide
> 2000-12-04  7:48 AM PST   Sunrise
>
> <--------->
>
> while (<INFILE>) {
>
>     ($year, $month, $mday, $hour, $minute, $am_pm, $tz, $height, $cond)
> =
>     ^(\d+)-(\d+)-(\d+)\s+(\d+):(\d+)\s+([A-Z]{2})\s+([A-Z]{3})\s+
>     ([0-9A-Za-z-.\s]{11})\s+(\w+\s+\w+)/;
>
>     $year and $started++;
>
>     if ($cond) {
>     ($year, $month, $mday, $hour, $minute, $am_pm, $tz, $cond) =
>     /^(\d+)-(\d+)-(\d+)\s+(\d+):(\d+)\s+([A-Z]{2})\s+([A-Z]{3})\s+
>     ([A-Za-z\s])/;
>
>     $date = "$year-$month-$mday";
>     $time = "$hour:$minute";
>     # Strip the leading and trailing spaces from $height
>     StripLTSpace($height);
>
>     printf OUTFILE "%s\,%s\,%s\,%s\,%s\,%s\n",
> $date, $time, $am_pm, $tz, $height, $cond;
>     }
>
> }
>
> $started or print STDERR "Didn't find a tides line";
>
> close(INFILE);
> close(OUTFILE);
> print STDERR "\n";
>
> 1;
>

Reply via email to