Universal read_data function



On June 2, 2011, Paul Bowyer wrote:
-------------------------------------------
The inclusion of one, or two lines of code in your function
  gives some protection against erroneous entries such as those
that occur by copy/paste or simply by hand-typed entry.
If I were writing this function, I'd do it this way:
[snip]
while (%l : readline(%s)) # false do
       ( %ln : %ln + 1,

         /*Added the following and the enclosing parens.
           The inclusion of these eliminates problems with:
           blank lines and CRs in the file. */

         /*Add this line if you're concerned about CRs in line ends.
         %l : strim(" ", ssubst(" ", ascii(13), %l ) ),*/

         if %l # "" then  /*Check for blank line*/
         (
           if %dsep = "text" then
  [snip]
------------------------------------------
Hi Paul, Thanks for the suggestion to include some
code to guard against empty lines. I have modified the
code to check that the string does not have zero length,
as in:
.......................
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)),
......................
I then created ndata9.dat initially with copy/paste as a six line file,
then with notepad2 (shift+cntrl+9) 'show end of line chars' mode
turned on, deleted all of lines 2, 4, and 6 up to (but not including)
the end of line chars and saved the file as is.
This resulted in a file with three empty lines, and their presence
is apparent from the output of printfile, which shows the
"blank" lines (because end of line chars are non-printable), as
in
---------------------------------
(%i1) (display2d:false,fpprintprec:8)$
(%i2) printfile("ndata9.dat")$
2 , 4.8, -3/4, "xyz", -2.8e-9

2 , 4.8, -3/4, "xyz", -2.8e-9

2 , 4.8, -3/4, "xyz", -2.8e-9

---------------------------------
Of course the same data file could have been created
by pasting one line, pressing Enter twice, pasting one
line, pressing Enter twice, pasting one line,
pressing Enter twice, and then saving the file as is.


The modified code now works around blank line defects in
a data file:

----------------------------
(%i16) load(readwrite);
(%o16) "c:/work2/readwrite.mac"
(%i17) read_data("ndata9.dat");
(%o17) [[2,4.8,-3/4,"xyz",-2.8E-9],[2,4.8,-3/4,"xyz",-2.8E-9],
        [2,4.8,-3/4,"xyz",-2.8E-9]]
-------------------------------

Ted