Universal read_data function



On 05/30/2011 09:59 AM, Edwin Woollett wrote:
> Hi Paul,
> Please send me your data files which fail with
> my code, as an attachement, so I get all the
> correct end of line chars in your files.
>
> Ted
>
> ----- Original Message ----- From: Paul Bowyer To: Edwin Woollett Cc: 
> Maxima List Sent: Sunday, May 29, 2011 5:34 PM
> Subject: Re: 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
>
Hi Ted:

Here are the data files I used with the read_data function. The names 
might not be the same as they were in your email message. I created 
ndata1.dat by using a hex editor (Okteta) on regular a text file that 
had extra space chars that could be modified. All the others were 
copy/pasted directly from your email message without modification. I use 
Thunderbird for email.

Paul

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ndata1.dat
URL: <http://www.math.utexas.edu/pipermail/maxima/attachments/20110530/a5f1d904/attachment.ksh>;
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ndata2.dat
URL: <http://www.math.utexas.edu/pipermail/maxima/attachments/20110530/a5f1d904/attachment-0001.ksh>;
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ndata3.dat
URL: <http://www.math.utexas.edu/pipermail/maxima/attachments/20110530/a5f1d904/attachment-0002.ksh>;
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ndata5.dat
URL: <http://www.math.utexas.edu/pipermail/maxima/attachments/20110530/a5f1d904/attachment-0003.ksh>;
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ndata6.dat
URL: <http://www.math.utexas.edu/pipermail/maxima/attachments/20110530/a5f1d904/attachment-0004.ksh>;