From: Pascal J. Bourguignon on
Aleksander Nabagło <n(a)ap.krakow.pl> writes:

> Yongwei Xing pisze:
>> Hi all
>>
>> I have a txt file like below:
>>
>> 1 2 3 4 5 6
>> 2 3 4 5 6 7
>> 8 9 1 7 6 7
>> 4 5 6 1 4 7
>>
>> How can I read these data into a nested list? If the data is string or
>> char, is there any difference to do it?
> Use array of arrays.
> Reformat lispy way and read again:
>
> ;;;
>
> (defun mtx-read (f)
> (read-from-string
> (with-output-to-string (s)
> (princ "#(" s)
> (flet ((rd () (read-line f nil nil)))
> (do ((g (rd) (rd)))
> ((or (null g)
> (every #'(lambda (x) (not (digit-char-p x))) g)))
> (format s "~A~A~A" "#(" g ")"))
> (princ ")" s)))))
>
> (with-open-file (f "m.dat" :direction :input)
> (print (mtx-read f)))

Well then, if you know the dimension of the matrix (you could read the
file a first time to get it), you could also read the data into a 2D
array. Well since the lines are not in parentheses, we will cheak,
reading a vector and displacing the 2D array onto it:


(defun read-matrix (stream cols rows)
(with-open-stream (matrix (make-concatenated-stream
(make-string-input-stream "#(")
stream
(make-string-input-stream ")")))
(let ((vector (read matrix)))
(make-array (list rows cols) :displaced-to vector))))

(with-input-from-string (example " 1 2 3 4 5 6
2 3 4 5 6 7
8 9 1 7 6 7
4 5 6 1 4 7
")
(read-matrix example 6 4))
--> #2A((1 2 3 4 5 6)
(2 3 4 5 6 7)
(8 9 1 7 6 7)
(4 5 6 1 4 7))


--
__Pascal Bourguignon__
http://www.informatimago.com
First  |  Prev  | 
Pages: 1 2
Prev: PAY PER CLICK
Next: Unexpected relative performance