Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting specific lines from a text file

Status
Not open for further replies.

johnismint

Programmer
Jan 21, 2009
3
GB
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
 
do this:

$periodline=$data[27];
print "$periodline]\n";

and see what gets printed. I suspect you are parsing the wrong line.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
remove the ']' from the print line, note sure how I got that in there.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Sorry I meant to add that I have printed that, and the error in the number appears in the $periodline, so it reads

# Period (days) : 0.540938

 
OK, then that is the value you will get when you split that line. There is no way perl is changing 0.540938 to something else all by itself. If it is then your perl installation is probably broken.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
You're welcome

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
@johnismint,

What was it that caused the issue?

the split should have treated the value as a string until told otherwise, because of the leading spaces, at least that's what I would have thought.

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top