I have a file that looks like this:
A B C D E
0 8 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With