Universal read_data function



Forgot to add maxima to the mailing.

-------- Original Message --------
Subject: 	Re: Universal read_data function
Date: 	Mon, 30 May 2011 13:47:08 -0700
From: 	Paul Bowyer <pbowyer at olynet.com>
To: 	Edwin Woollett <woollett at charter.net>



On 05/30/2011 01:04 PM, Edwin Woollett wrote:
>  Attached is pb-data1.dat which is
>  your ndata1.dat supplemented with the
>  symbols CRLF everywhere I found
>  them in the file, using notepad2
>  (shift-ctrl-9).
>  a. you have incorrect CRLF between lines 6 and 7
>  b. you have incorrect CRLF between lines 7 and 8
>  c. you have an incorrect (ie gives error) CRLF
>  after the last line ending CRLF.
>
>  In all the above case, you have back to
>  back CRLF's  which will give trouble when you try to read in lines.
>
>  ted
>
>
>  ----- Original Message ----- From: Paul Bowyer To: Edwin Woollett Cc:
>  Maxima List Sent: Monday, May 30, 2011 12:09 PM
>  Subject: Re: 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
Ted:

I looked at pb-data1.dat in Okteta and the places you changed to 'CR'
and 'LF' coincide with their locations in ndata1.dat. In addition,
pb-data1.dat follows 'CR' and 'LF' with a hex 0x0A, a LF in hexadecimal,
but no hexadecimal CR is present. All of the files I copy/pasted from
your email message show a sequence of hex 0x0D 0x0A.

Am I mistaken to think the normal Windows line endings for text files
are hex 0x0D 0x0A ?

The back to back CR/LF are just empty lines occuring in the data file
which the additional lines in the read_data function took care of on my
Linux box.

Paul