assuming i'm understanding correctly what you want, you might want to try this:
use strict;
use vars qw(@store);
open I, "input.txt" or die "oops: $!\n";
while(<I>){ my @foo = /(.+?)\s+(.+?)\s{2,}(.+?)(?:\s{2,}(.+))?$/; push @store,
\@foo }
@store will now hold all matches on your input file, which are references to
arrays.
you can acces the references as follows, with the format of the array referred
to like this:
for(@store) {
$_->[0] # is the date
$_->[1] # is the time
$_->[2] # is either 'x quarter | sunrise, etc' or 'x feet'
$_->[3] # will hold the tide indication if there is one
}
you can print them out as a csv file as follows:
for(@store){ print ( (join ',', @$_) . "\n")}
this is a bit advanced regexes, but you can check out a tutorial on them at
www.sharemation.com/~perl/tut to give you an insight in them
hth,
Jos Boumans
Ken wrote:
> 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;
> >