Hi
as a relative newcomer to Lisp and especially newLisp I would like to ask how a function in good style looks like which does the following:
It reads in a text file line by line and gives back a list of all this lines.
Call
(read-textfile-into-list "myFile.txt")
There is a partial answer in the documentation
http://www.newlisp.org/downloads/manual_frame.html
command open
and there is a read-line function. However I do not know how to properly properly initialize the list? Which functions should I use to append to the list?
Regards
HJH
Hello HJH,
(setq list-of-lines (parse (read-file "myfile.txt") "rn"))
will do the job in which 'read-file' reads the entire file as a big ol' string, 'parse' with "rn" separates the string into a list of lines (use "n" instead of "rn" if not Windows), and 'setq' associates the resulting list with the symbol 'list-of-lines'.
Quote from: "Sammo"
(setq list-of-lines (parse (read-file "myfile.txt") "rn"))
Thank you Sammo. This is very compact and readable at the same time.
It works fine and what is exciting: It reads UTF8 characters right out of the box (well, I had to install the exe).
For the number of files I am dealing with this solution is fine (100...400 files, 5kB each) but if somebody has a more conventional solution with read-line I am curious to know it as well.
HJH
(setq list-of-lines '())
(setq myfile (open "myfile.txt" "read"))
(while (read-line myfile)
(push (current-line) list-of-lines -1))
(close myfile)