johnismint
Programmer
Hello, I am having a little trouble with the following program. I have a text file like this:
...
# PEAK : 1
# Period (days) : 0.541010
# Offset : 0.0009
# Amplitude : 0.0350
# Phase : 0.3394
# Delta chisq : 84206.
# T0 : 0.
# Gap Fraction : 0.0012
...
where I need to extract the numerical values after the ':' sign. I have the following code that reads in the entire file, splits the lines I want on the ':' character, and then puts them into a variable without the text like so:
open $fh, $filename or die "Failed to open file '$filename': $!";
my @data = <$fh>; # read all lines into array @data
close $fh;
$periodline=$data[27];
my @values = split('\:', $periodline); # Create array @values for two parts of $periodline
$values[1] =~ s/^\s+//; # Remove whitespace
$period = $values[1]; # Set as variable
chomp $period;
However, the actual numerical values that are then printed after going through this procedure are slightly different than the original in the text file (0.541393 instead of 0.541010). I assume this is something to do with the fact that I'm passing the data into several different variables, but I was wondering if anyone could think of a way to retain the accuracy of the original number?
Many thanks, John
...
# PEAK : 1
# Period (days) : 0.541010
# Offset : 0.0009
# Amplitude : 0.0350
# Phase : 0.3394
# Delta chisq : 84206.
# T0 : 0.
# Gap Fraction : 0.0012
...
where I need to extract the numerical values after the ':' sign. I have the following code that reads in the entire file, splits the lines I want on the ':' character, and then puts them into a variable without the text like so:
open $fh, $filename or die "Failed to open file '$filename': $!";
my @data = <$fh>; # read all lines into array @data
close $fh;
$periodline=$data[27];
my @values = split('\:', $periodline); # Create array @values for two parts of $periodline
$values[1] =~ s/^\s+//; # Remove whitespace
$period = $values[1]; # Set as variable
chomp $period;
However, the actual numerical values that are then printed after going through this procedure are slightly different than the original in the text file (0.541393 instead of 0.541010). I assume this is something to do with the fact that I'm passing the data into several different variables, but I was wondering if anyone could think of a way to retain the accuracy of the original number?
Many thanks, John