#1
|
|||
|
|||
read file and place line in property list
Hello,
I want to make a LISP function to read a line of a file and place it in a property list. Code:
(defun read-param-file (&key paramfile) ;function: read param file ;date : 03-05-2004 ;---------------------------------------------------------------------- ;input : :paramfile [string] , name parameter file (progn ;local vars (let (a_line a_list) ;read file and fill list variable (with-open-file (input-stream paramfile :direction :input) (loop (setf a_line (read-line input-stream nil 'eof)) (when (eq a_line 'eof) (loop-finish)) (push a_line a_list) );loop ) );let );progn );defun Code:
:objectfile "Z:/users/harry/lisp/utils/models/assy1.pkg" :vx 0 :vy 0 :vz 0 :ax 0 :ay 0 :az 0 :objectfile "Z:/users/harry/lisp/utils/models/assy2.pkg" :vx 100 :vy 200 :vz 50 :ax 0 :ay 0 :az 0 I have to convert a_line into a property list. Has anyone done this before? |
#2
|
|||
|
|||
Hello Harry,
I haven't done this before. But here are some commands you could use. * (sd-string-split a-line " ") splits up your string into a list of single strings. Every single string can be handled by itself. * (read-from-string a-string) converts the data from string to keyword or number or whatever Perhaps this helps you to come a step further. Dorothea |
#3
|
|||
|
|||
Hi Harry,
You can make life much easier if you are able to change the parameter file to have parentheses around each record i.e. Code:
(:objectfile "Z:/users/harry/lisp/utils/models/assy1.pkg" :vx 0 :vy 0 :vz 0 :ax 0 :ay 0 :az 0) (:objectfile "Z:/users/harry/lisp/utils/models/assy2.pkg" :vx 100 :vy 200 :vz 50 :ax 0 :ay 0 :az 0) Code:
(in-package :test) (use-package :oli) (defun read-param-file (fname) (let ((paramlist) ; this will store the records (data) ; temp location for each record ) (with-open-file (f fname :direction :input) (loop while (setf data (read f nil)) do (setq paramlist (append paramlist (list data) ) ;append ) ; setq ) ; loop ) ; with-open-file ;; now display the list we've collected for verification (display paramlist) paramlist ; returns list to calling function ) ) After calling the above function, you can get access to the individual data records in the list using any of the list operators such as nth, first, dolist, etc. For example: Code:
(defun show-filenames (fname) (let ((paramlist)) (setq paramlist (read-param-file fname)) (dolist (data paramlist) ;iterates over paramlist (display (format nil "Filename: ~A" (getf data :objectfile))) ) ) ) Filename: Z:/users/harry/lisp/utils/models/assy1.pkg Filename: Z:/users/harry/lisp/utils/models/assy2.pkg from the data above (when given the right filename, of course). Notice that this keeps your property list intact for each record -- all of the reference keywords are used just as you entered them in the data file, and you don't have to do any fancy manipulation to get them organized. If this isn't clear, please let me know. I hope there is enough detail above to help you get what you need. If you are not able to put the parens in your parameter file, Dorthea's suggestions are a good workaround, though it's much nicer to let the lisp reader do the work for you! Good luck! andy
__________________
Andy Poulsen AI MAXTools: Dream. Design. Done. It's that easy! Add-ins bringing new functionality and speed to Creo Elements/Direct and CoCreate products. Now available for v17-v20+! See them in action at www.ai-maxtools.com and then try them for yourself -- FREE! Last edited by Andy Poulsen; 05-04-2004 at 10:54 AM. |
#4
|
|||
|
|||
Thanks Dorothea and Andy for your help.
I could solve the problem. Best regards Harry |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|