Read data file containing fractional numbers?



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