开发者

Convert a lisp string to stream

I have a file that looks like this:

A B C D E
0 8开发者_如何学JAVA 6 12 5
8 0 10 8 9
6 10 0 7 11
12 8 7 0 6
5 9 11 6 0

I don't know ahead of time how many rows and columns there will be. I would like to read the top line, which will let me know the number of rows to expect . I found lisp's (read <stream>) function which, in a loop, can parse each of the characters as symbols. I have not found a way, however, to limit the loop to only reading the first line and stopping there. The solution I'm trying to make work would be something like

(with-open-file (stream "/home/doppler/tmp/testcase1.txt")
  (setf line (read-line stream))
  (when line
    (loop for symbol = (read line nil)
         while symbol do (print symbol))))

The problem with this is that (read-line stream) returns a string which cannot be parsed by (read line nil) to extract the symbols (s-expressions).

How can I either convert the string line to a stream, or, if possible, extract the symbols directly from the string?


You can either use the string as a stream by using the with-input-from-string macro, or use read-from-string in a loop. You may also be interested in the read-delimited-list function, although you would have to add some kind of delimiter character to the end of the string before using it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜