Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LISP - Converting a string with the representation of an array to an array

I have a file which has an bi-dimensional array in each line. I'm having trouble finding a way of parsing the file into actual arrays and them putting them in a list.

the file looks like this, the arrays are in different lines, even though it does not seem to: file with matrixes

like image 320
Lidia Freitas Avatar asked Dec 08 '15 12:12

Lidia Freitas


1 Answers

You just open the file for reading using with-open-file and then use the function read as often as you would like or as often as there are arrays. Each read returns an array. Using loop you can collect them into a list.

Basically something like this:

(with-open-file (s filename)
  (let ((*read-eval* nil))
    (loop with eof = '#:eof
          for object = (read s nil eof)
          until (eq object eof)
          collect object)))

Note also that it does not matter whether each array is on its own line. It would still work if they are on one line. A newline between expressions is just whitespace for the Lisp reader.

like image 195
Rainer Joswig Avatar answered Oct 20 '22 12:10

Rainer Joswig