Subject: Read data file containing fractional numbers?
From: Edwin Woollett
Date: Mon, 23 May 2011 14:22:28 -0700
On May 22, 2011, Paul Bowyer wrote:
--------------------------------------------------
I modified it again like this to break out the various parts and add a
close statement:
read_data1(%filename) := block([%s, %r],
if not file_search(%filename) then
(disp(" file not found "), return(false)),
%s : openr(%filename),
%r : [],
while (l : readline(%s)) # false do
(
/*%r : cons(map(parse_string, split(l)), %r),*/
%r : l,
%r : split (%r),
%r : parse_string ( %r ), /*chokes here. Need something different!*/
disp( %r )
/*reverse(%r)*/
),
close(%s)
);
mydata1.dat contains (unquoted and without newlines in long expression,
but with internal spaces):
10.3
14.4
read_data1(%filename):=block([%s,%r],if notfile_search(%filename) then
(disp(" file not found "),return(false))
,%s:openr(%filename),%r:[],while (l:readline(%s))#false do
%r:cons(map(parse_string,split(l)),%r),reverse(%r))
12.6
14.5
called as:
read_data1("/home/pfb/mydata1.dat");
and the output was:
parse_string: ["10.3"] is not a string.
#0: read_data1(%filename=/home/pfb/mydata1.dat)
-- an error. To debug this try: debugmode(true);
Paul
-----------------------------------
Hi Paul, and thanks for catching my dumb typos.
The following version of read_data1 works with all data files I have
tested, decimal numbers, fractional numbers, and doesn't care
about text file line endings.
read_data1(%filename) := block ([%s,%r,%l],
if not file_search (%filename) then
(disp (" file not found "),return (false)),
%s : openr (%filename),
%r : [],
while (%l : readline(%s)) # false do
( %r : cons (map(parse_string, split(%l)), %r)),
close (%s),
reverse (%r))$
Ted