I forgot the maxima list again...
-------- Original Message --------
Subject: Re: Universal read_data function
Date: Sat, 04 Jun 2011 15:45:43 -0700
From: Paul Bowyer <pbowyer at olynet.com>
To: Edwin Woollett <woollett at charter.net>
On 06/04/2011 12:18 PM, Edwin Woollett wrote:
> On June 3, 2011, Paul Bowyer wrote:
> --------------------------------
> [snip]
> With the CRLF sequences in the file, the read_data function chokes on
> the seemingly blank lines with the CRLF sequence whether I use:
> "if %l # "" then"
> or
> "if slength(%l) # 0 then"
>
> as the zero-length test
>
> I had to include:
> "%l : strim(" ", ssubst(" ", ascii(13), %l ) ),"
> just prior to the zero-length test for proper operation.
>
> A simple work-around for linux/unix users is to run, from a terminal
> window:
> dos2unix<filename> on the data file if dos2unix is installed.
> -------------------------------
>
> Have you tried my last version of read_data(?) which,
> (in response to your suggestion) simply ignores any and
> all blank lines, as a good read_data function ought to
> do anyway, and also doesn't care about choice of
> end of line chars.
> (see below)
>
> Ted
> -------------------------
> 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
> if slength(%l) # 0 then
> ( %ln : %ln + 1,
> if %dsep = "text" then
> %r : cons (%l,%r)
> else if not lfreeof (%whole,%ln) then
> %r : cons (%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:
That's the function I tried. The problem, at least on a Linux machine,
is that
"slength(%l)" returns 1 rather than 0 when there is a blank line with a
CRLF line end because it sees the CR.
Testing against [%l # ""] would have the same difficulty because of the
CR in the string.
I don't know if that is true for a Windows machine because I rarely use
Windows any more.
I have Windows XP on my Linux system, but it's been so long since I've
booted it that I can't get it to boot any longer. I think that's because
I added a drive and some partitions and Windows XP wants things a
particular way before it will boot. I just haven't taken the time to see
if I can get it to boot and still keep my Linux installations booting.
It might be the case that Maxima on a Windows machine never sees the
CRLF sequences and they are only a problem on Linux machines where the
default line-end is a simple LF. If your read_data function on your
Windows machine never sees a CR, then you would find it difficult to
write code to catch one.
Paul