[Brisbane-pm] Regex Syntax

Jacinta Richardson jarich at perltraining.com.au
Thu Apr 12 20:21:15 PDT 2007


Anthony Thyssen wrote:
> Jacinta Richardson on  wrote...

> | open FILE, "<", $name or die $!;
> | 
> | my @records;
> | while(<FILE>) {
> | 	my ($date, $time, $rainfall) = split (/ /, $_);
> | 
> | 	my ($day, $month, $year) = split("/", $date);
> | 
> | 	# Sometimes we're not given time at all
> | 	my ($hours, $min, $sec) = (0,0,0);
> | 	if( !$rainfall ) {
> | 		$rainfall = $time;
> | 	}
> | 	else {
> | 		($hours, $min, $sec)  = split(":", $time);
> |   	}
> | 	
> | 
> | 	push @records, {
> | 		day	=> $day,
> | 		month	=> $month,
> | 		year	=> $year,
> | 		hour    => $hours,
> | 		minute	=> $minutes  || 0,    # sometimes no minutes
> | 		second	=> $second   || 0,    # sometimes no seconds
> | 		rain	=> $rain,
> | 	};
> | }
> | 
> Hmmm...  Small bug in the above.
> 
> If time was not given. then hours is set to the rainfall,
> and also needs to be reset to 0.

How would that happen?  If time is not given then the value for $rainfall will
have been put into $time.  So we do:

 	if( !$rainfall ) {
 		$rainfall = $time;
 	}

and hours remains untouched (thus set to 0 from the initialiser above).

Have I missed something?

Further, the first split should probably be: split (/\s+/, $_);  and if the
rainfall can ever be zero we should probably write:

	if( not defined $rainfall ) {
		$rainfall = $time;
	}

All the best,

	Jacinta

-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |


More information about the Brisbane-pm mailing list