File text to LISP list

Discussion in 'AutoCAD' started by Carl & Lisa, Nov 23, 2003.

  1. Carl & Lisa

    Carl & Lisa Guest

    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
     
    Carl & Lisa, Nov 23, 2003
    #1
  2. 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
     
    Joseph Willis, Nov 24, 2003
    #2
  3. Carl & Lisa

    Tom Berger Guest

    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
     
    Tom Berger, Nov 24, 2003
    #3
  4. Carl & Lisa

    Carl Guest

    Joseph and Tom:

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

    Carl
     
    Carl, Nov 25, 2003
    #4
  5. !#$!%!$@^@%&#&#&&^#&@#&^&#%$@#%%$ you!!!!!!!!!!!









     
    Joseph Willis, Nov 28, 2003
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.