[Brisbane-pm] Regex Syntax

Martin Jacobs martin_jacobs at optusnet.com.au
Thu Apr 12 01:07:47 PDT 2007


Hi folks,

I've got a clumsy way to do regexes, and I'm looking for a better way.

I need to read the data from a file (called $name), which has the  
following format...

    Date   Rainfall mm
01/01/2000 00:00:00          27.333
01/01/2000 00:05:00         0.0
01/01/2000 02:00:00        29.15
01/01/2000 02:05:30        0.0



The way I've been doing it to date is

	open (RF, "<", "$name") or die "PERRMOSS could not open file: $name!";
	my @file     = <RF>;
	chomp @file;

Which cuts of whatever newline character there is on each line,  
followed by

	for $i (1..$#file){
	$file[$i] =~ m|(\d+)\/(\d+)\/(\d{4})\s+(\d+):(\d+):(\d+)\s*(\d.\d*)|) {
		($day,$month,$year,$hour,$minute,$second,$rain) = ($1,$2,$3,$4,$5, 
$6,$7);
		}
		}

To accommodate some variations in the input record, I have expanded  
this to

	for $i (1..$#file){
	if ($file[$i] =~ m|(\d+)\/(\d+)\/(\d{4})\s+(\d+):(\d+):(\d+)\s*(\d. 
\d*)|) {
		($day,$month,$year,$hour,$minute,$second,$rain) = ($1,$2,$3,$4,$5, 
$6,$7);
		}
	elsif ($file[$i] =~ m|(\d+)\/(\d+)\/(\d{4})\s+(\d+):(\d+)\s*(\d. 
\d*)|) {
		($day,$month,$year,$hour,$minute,$rain) = ($1,$2,$3,$4,$5,$6);
		$second = 0;}
	elsif ($file[$i] =~ m|(\d+)\/(\d+)\/(\d{4})\s+(\d+)\s*(\d.\d*)|) {
		($day,$month,$year,$hour,$rain) = ($1,$2,$3,$4,$5);
		($second,$minute) = (0,0);
		}	
	elsif ($file[$i] =~ m|(\d+)\/(\d+)\/(\d{4})\s*(\d.\d*)|) {
		($day,$month,$year,$rain) = ($1,$2,$3,$4);
		($second,$minute,$hour) = (0,0,0);
		}	
	else {print_to [$Screen,$summary], "
PERRMOSS cannot read rainfall data file $name near line $i
PERRMOSS aborted at: \t\t$fulltime\n\n";
		exit;}
		}



The problem is that the first value of $rain should equal 27.333, but  
it equals 27. So, there's a syntax issue, and i would be grateful for  
any hints.

In terms of the bigger picture, is using $1,$2 etc the best way to do  
it?





Regards,
Martin
Visit my website...
http://web.mac.com/martin_jacobs1


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/brisbane-pm/attachments/20070412/e68c4bc5/attachment.html 


More information about the Brisbane-pm mailing list