Universal read_data function



On 05/29/2011 12:55 PM, Edwin Woollett wrote:
> I meant to include the code for
> the four arg version of read_data:
>
> -----------------------------------------------
>
> read_data([%v]) :=
>    block ([%s,%r,%l,%filename,%dsep,%mult:true,%mix:false,
>              %whole:[],%ln],
>
>     %filename : part (%v,1),
>
>     if not stringp (%filename)
>       then ( disp (" file name must be a Maxima string "),
>              return (false)),
>
>    if not file_search (%filename) then
>      (disp (" file not found "),return (false)),
>
>    if length (%v) = 1 then %mix : true
>       else if length(%v) = 2 then %dsep : part (%v,2)
>       else if length (%v) = 3 then (%dsep : part (%v,2), %mult : part 
> (%v,3))
>       else (%dsep : part (%v,2), %mult : part (%v,3),%whole : 
> part(%v,4)),
>
>
>
>    %s : openr (%filename),
>    %r : [],
>    %ln : 0,
>
>    while (%l : readline(%s)) # false do
>       ( %ln : %ln + 1,
>         if not lfreeof (%whole,%ln) then
>            %r : cons (parse_string (%l),%r)
>         else if %mix then
>            %r : cons (map(parse_string, split(ssubst (" ",",",%l))), %r)
>         else %r : cons (map(parse_string, split(%l,%dsep,%mult)), %r)),
>
>    close (%s),
>    reverse (%r))$
>
> -----------------
>
> ted
>
>
>
>
Hi Ted:

I tried the above listed read_data function and it was still choking on 
the CRs in the data files on my Linux box.
I fiddled with it slightly and this is what it looks like now:

read_data([%v]) := block (
   [%s,%r,%l,%filename,%dsep,%mult:true,%mix:false,%whole:[],%ln ],
     %filename : part (%v,1),
     if not stringp (%filename)
       then ( disp (" file name must be a Maxima string "),
              return (false)),
    if not file_search (%filename) then
      (disp (" file not found "),return (false)),
    if length (%v) = 1 then %mix : true
       else if length(%v) = 2 then %dsep : part (%v,2)
       else if length(%v) = 3 then (%dsep : part (%v,2), %mult : part 
(%v,3))
       else (%dsep : part (%v,2), %mult : part (%v,3),%whole : part(%v,4)),

    %s : openr (%filename),
    %r : [],
    %ln : 0,
    while (%l : readline(%s)) # false do
       ( %ln : %ln + 1,

         /*Added the following two lines and the enclosing parens*/
         %l : strim(" ", ssubst(" ", ascii(13), %l ) ),
         if %l # "" then
         (
           if not lfreeof (%whole,%ln) then
            %r : cons (parse_string (%l),%r)
           else if %mix then
            %r : cons (map(parse_string, split(ssubst (" ",",",%l))), %r)
           else
             %r : cons (map(parse_string, split(%l,%dsep,%mult)), %r)
         )
       ),
    close (%s),
    reverse (%r));

Now it works on my Linux box, but you'll need to check it on your 
Windows box. I tested it against all of the data files you listed in 
your message where you forgot to post the code.

I started to go back and modify the ?read\-char method I submitted 
earlier, but I soon discovered it was more work than I wanted to do to 
get it to automatically determine the data types it was reading. Maybe 
I'll play more with that another time just for fun. I was having 
difficulty trying to catch errors in Maxima so I could gracefully close 
the file and exit, but I wasn't able to do that either.

Paul