Subject: Read data file containing fractional numbers?
From: Paul Bowyer
Date: Sun, 22 May 2011 11:29:35 -0700
On 05/21/2011 06:14 PM, Paul Bowyer wrote:
> On 05/21/2011 02:23 PM, Edwin Woollett wrote:
>> trying to include the opening (and closing? )
>> of the requested data file inside a block
>> structure, and getting errors, as in:
>> ======================
>> Maxima 5.24.0 http://maxima.sourceforge.net
>> using Lisp GNU Common Lisp (GCL) GCL 2.6.8 (a.k.a. GCL)
>> Distributed under the GNU Public License. See the file COPYING.
>> Dedicated to the memory of William Schelter.
>> The function bug_report() provides bug reporting information.
>> 2011-05-21
>>
>>
>> (%i1) load(read_data1);
>> (%o1) c:/work2/read_data1.mac
>> (%i2) fundef (read_data1);
>> (%o2) 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), reverse(%r))
>> (%i3) read_data1("mydata1.dat");
>> readline: argument must be a stream.
>> #0: read_data1(%filename=mydata1.dat)(read_data1.mac line 13)
>> -- an error. To debug this try: debugmode(true);
>> ==========================
>> lines 13 and 14 of the data file are :
>>
>> while (l : readline(s)) # false do
>> ( r : cons (map(parse_string, split(l)), r)),
>> ==============================
>> So the file hasn't been opened for reading by
>> my code?
>>
>>
>> Ted
>>
>>
>> _______________________________________________
>> Maxima mailing list
>> Maxima at math.utexas.edu
>> http://www.math.utexas.edu/mailman/listinfo/maxima
>>
> Hi Ted:
>
> I changed your syntax slightly to add some missing % chars and got it
> to work with this:
> 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),
> reverse(%r)
> );
>
> I called it with:
> read_data1("/home/pfb/mydata1.dat");
>
> But, when I include more than just simple data in the data file, it
> chokes:
>
> If mydata1.dat contains (No newlines in long expression ):
> 10.3
> 14.4
> 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), reverse(%r));
> 12.6
> 14.5
> It gives this error:
> stdin:2:incorrect syntax: := is not a prefix operator
>
> If mydata1.dat contains (No newlines in long expression -- copy pasted
> from fundef (read_data1) output):
> 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
> It gives this error:
> stdin:40:incorrect syntax: Premature termination of input at $.
>
> If mydata1.dat contains (No newlines in long expression, quoted
> expression, and escaped " chars):
> 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
> It gives this error, which seems to say it got past readline, but now
> chokes at parsing:
> Maxima encountered a Lisp error:
>
> Error in function PEEK-ONE-TOKEN-G:
> parser: end of file while scanning expression.
> Automatically continuing.
> To enable the Lisp debugger set *debugger-hook* to nil.
>
>
> If mydata1.dat contains:
> 10.3
> 14.4
> 12.6
> 14.5
> it works fine.
>
> It looks like you need to quote your expressions and then pre-process
> them them before your run them into the parser, but I'm uncertain if
> that's true.
>
> Paul
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>
Hi again Ted:
I fiddled with this a little more this morning when I had some time so I
could understand what's happening.
Part of the problem last night was there wasn't a closing of the stream,
so maxima kept opening new steams each time I ran the function.
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