reading data from a file



Poul Riis wrote:

> I have four coloumns of data in a file. How can I read 
> these data into two arrays A[2,n] and B[2,n]?

There doesn't seem to be a built-in function. I've wanted
to be able to do that operation also. So I wrote the following
simple functions which I think will be useful to you.
The code is attached to this message. Do LOAD("foo.lisp");
or TO_LISP(); ---(paste in code)--- (to-maxima) 
to get the functions defined.

READ_NESTED_LIST makes a list out of each row, and then a list
of the rows. The rows can have different numbers of elements.
This is sometimes called a "ragged array".

READ_MATRIX returns a matrix. Each row must have the same
number of elements. The matrix can have number of columns
different from number of rows (i.e., not a square matrix).

These functions will read in all the data at once.
You'll need to work with for-loops or COL or something
to split it apart.

Hope this helps,
Robert Dodier

--------------------- >8 ---------------------
;; Usage: A:READ_MATRIX("foo.data")$

(defun $read_matrix (file-name)
  (mapply-tr '$matrix ($read_nested_list file-name)))


;; Usage: A:READ_NESTED_LIST("foo.data")$

(defun $read_nested_list (file-name)
  (setq a '())
  (with-open-file (in (stripdollar file-name))
    (loop
      (setq l (read-line in nil 'eof))
      (if (eq l 'eof) (return (cons '(mlist simp) a)))
      (setq a (append a (list (make-mlist-from-string l)))))))


;; Usage: (make-mlist-from-string "1 2 3 foo bar baz")
;; Write ?FOO, ?BAR, ?BAZ to manipulate atoms in Maxima

(defun make-mlist-from-string (s) 
  (setq r '()) 
  (setq in (make-string-input-stream s)) 
  (loop 
    (setq x (read in nil 'eof)) 
    (if (eq x 'eof) (return  (cons '(mlist simp) r))) 
    (setq r (append r (list x)))))
--------------------- 8< ---------------------


__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus