File text to LISP list

Carl Lisa

Usenet Archive
Joined
Nov 22, 2003
Messages
1
Reaction score
0
Hi!

I am trying to read lines of text from a .txt file and place them into a list identified by one LISP variable.

I can open the file for reading. I can read a line and assign it a variable. I cannot figure out how to read subsequent lines (loop) and add them to the variable.

Any help (or direction) would be greatly appreciated.

Thanks, Carl
 
Hi Carl & Lisa

This autolisp function will read a text file from start to finish and assign the data as a list to the autolisp variable LST1. You then can manipulate the list any way you want.

(defun FILELIST (DATAFILE / DATA LST2 ALL)
(setq DATA (open DATAFILE "r"))
(setq LST1 (list ""))
(while (setq ALL (read-line DATA))
(setq LST2 (list ALL))
(setq LST1 (append LST1 LST2))
)
(close DATA)
(setq LST1 (cdr LST1))
)

EXAMPLE OF USAGE
(FILELIST "C:/TEMP/TEST.TXT")

This is a function that requires one argument passed to it.

Good Luck
Joseph E. Willis
 
This autolisp function will read a text file from start to finish and assign the data as a list to the autolisp variable LST1. You then can manipulate the list any way you want.

(defun FILELIST (DATAFILE / DATA LST2 ALL)
[...]

Do you allow me to improve your function a little bit and make it more "Lispish"? Here it is:

(defun filelist (datafile / filehandle line data)
(if (and (findfile datafile)
(setq filehandle (open datafile "r"))
)
(progn
(while (setq line (read-line filehandle))
(setq data (cons line data))
)
(close filehandle)
)
)
(reverse data)
)

EXAMPLE OF USAGE
(setq lst1 (FILELIST "C:/TEMP/TEST.TXT"))

Cheers
Tom Berger
 
Joseph and Tom:

Thank you both very very much. Your little functions worked beautifully and was exactly what I needed!

Carl
 
!#$!%!$@^@%&#&#&&^#&@#&^&#%$@#%%$ you!!!!!!!!!!!

Tom Berger said:
This autolisp function will read a text file from start to finish and assign the data as a list to the autolisp variable LST1. You then can manipulate the list any way you want.

(defun FILELIST (DATAFILE / DATA LST2 ALL)
[...]

Do you allow me to improve your function a little bit and make it more "Lispish"? Here it is:

(defun filelist (datafile / filehandle line data)
(if (and (findfile datafile)
(setq filehandle (open datafile "r"))
)
(progn
(while (setq line (read-line filehandle))
(setq data (cons line data))
)
(close filehandle)
)
)
(reverse data)
)

EXAMPLE OF USAGE
(setq lst1 (FILELIST "C:/TEMP/TEST.TXT"))

Cheers
Tom Berger
 

Latest threads

Back
Top