On May 28, 2011, Paul Bowyer wrote:
-------------------------------------------------
My data file (with embedded CRs, LFs, and spaces, which probably don't
show up in this message, but I can attach it if necessary):
2.3e9 "Abc"
10.3
14.4
read_data1(%filename):=block([%s,%r],if notfile_search(%filename) then
(disp(" file not found "),return(false))
,%s:openr(%filename),%r:[],while (l:readline(%s))#false do
%r:cons(map(parse_string,split(l)),%r),reverse(%r))
12.6
14.5
----------------------------------
Thanks for the good example of a complicated data file.
I am going to assume you want all of the function definition
as one separate data item, so make sure it is all on one line.
(ie, look at the hidden end of line chars to make sure it is
all on one line (for example with notepad2).)
See my example ndata8.dat below.
To include cases in which you want whole lines treated as one data
item, regardless of the space, commas,etc, I have included an optional
fourth arg in read_data, to be used in such an eventuality.
Thus the most complicated syntax might look like:
read_data ("ndata10.dat", ";" , true, [2,3,11] )
meaning treat lines 2,3,and 11 as single data items, and for
the other lines use ";" as the data item separator.
------------------------------------------------------------
Maxima 5.24.0 http://maxima.sourceforge.net
using Lisp GNU Common Lisp (GCL) GCL 2.6.8 (a.k.a. GCL)
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
2011-05-29
(%i1) (display2d:false,fpprintprec:8)$
(%i2) load(readwrite);
(%o2) "c:/work2/readwrite.mac"
(%i3) printfile ("ndata2.dat")$
2 , 4.8, -3/4, "xyz", -2.8e-9
3 22.2 7/8 "abc" 4.4e10
(%i4) read_data ("ndata2.dat");
(%o4) [[2,4.8,-3/4,"xyz",-2.8E-9],[3,22.2,7/8,"abc",4.4E+10]]
(%i5) printfile ("ndata3.dat")$
2.0; -3/7; (x:1,y:2,x+y); block([fpprec:24],bfloat(%pi)); foo
(%i6) read_data ("ndata3.dat",";");
(%o6) [[2.0,-3/7,(x:1,y:2,y+x),block([fpprec:24],bfloat(%pi)),foo]]
(%i7) printfile ("ndata5.dat")$
1 0
2 -1/3
3 -1/2
4 -3/5
5 -2/3
(%i8) read_data ("ndata5.dat");
(%o8) [[1,0],[2,-1/3],[3,-1/2],[4,-3/5],[5,-2/3]]
(%i9) printfile ("ndata6.dat")$
1, 2/3, 3.4, 2.3e9, "file ndata6.dat"
"line two" , -3/4 , 6 , -4.8e-7 , 5.5
7/13, "hi there", 8, 3.3e4, -7.3
4,-3/9,"Jkl", 44.6, 9.9e-6
(%i10) read_data("ndata6.dat",",");
(%o10) [[1,2/3,3.4,2.3E+9,"file ndata6.dat"],["line
two",-3/4,6,-4.8E-7,5.5],
[7/13,"hi there",8,33000.0,-7.3],[4,-1/3,"Jkl",44.6,9.9E-6]]
(%i11) printfile("ndata8.dat")$
2.3e9 "Abc"
10.3
14.4
read_data1(%filename):=block([%s,%r],if notfile_search(%filename) then
(disp(" file not found "),return(false)) ,%s:openr(%filename),%r:[],while
(l:readline(%s))#false do
%r:cons(map(parse_string,split(l)),%r),reverse(%r))
12.6
14.5
(%i12) read_data ("ndata8.dat"," ",true,[4]);
(%o12) [[2.3E+9,"Abc"],[10.3],[14.4],
read_data1(%filename):=block([%s,%r],
if notfile_search(%filename)
then (disp(" file not found "),return(false)),
%s:openr(%filename),%r:[],
while (l:readline(%s)) # false do
%r:cons(map(parse_string,split(l)),%r),reverse(%r)),
[12.6],[14.5]]
------------------------------------
Needless to say, there really will never be a
truly "universal" anything, including read_data,
but you are always welcome to roll your own,
depending on your specialized needs.
Ted Woollett