AbdRF Posted March 8, 2020 Posted March 8, 2020 Hi all, I have been learning to retrieve external data (from .dat file), format it into something that Autolisp can use, then place the data into individual variables from afralisp site .I am not able to properly understand the below part of the code especially the substr part . I am also attaching the source code lisp and dat file (if data ;if the size has been found (progn (setq maxs (strlen data) ;establish length of input count 1 ;initiliaze count chrct 1 ;initiliaze char position );setq (while (< count maxs) ;process string one chr at a time (if (/= "," (substr data count 1)) ;look for the commas (setq chrct (1+ chrct)) ;increment to next position (setq numb (atof (substr data (1+ (- count chrct)) chrct)) ;convert to real dlist (append dlist (list numb)) ;add it to the list chrct 1 ;resets field ct );setq );if (setq count (1+ count)) ;increment the counter );while (setq numb (atof (substr data (1+ (- count chrct)))) ;convert to real dlist (append dlist (list numb)) ;add it to the list );setq );progn );if data Test.lspTest.dat Thanks Quote
BIGAL Posted March 8, 2020 Posted March 8, 2020 Quickest answer is use lee mac parse CSV. Every second line read line then ; thanks to www.Lee-mac.com Parse csv for this defun (defun csv->lst ( str / pos ) (if (setq pos (vl-string-position 44 str)) (cons (substr str 1 pos) (csv->lst (substr str (+ pos 2)))) (list str) ) ) (setq lst '()) (setq newl (read-line fo)) (setq lst (cons (csv->lst newl) lst)) I think rewritten would be about 1/2 code if read line = *x read line else read-line, read-line back in test loop. Quote
BIGAL Posted March 9, 2020 Posted March 9, 2020 I had a go at it, in your code you said 1st line is a heading but your dat is not so l changed dat to match. also made a b c to be numbers. ; thanks to www.Lee-mac.com Parse csv for this defun ; (chr 44) is "," (defun csv->lst ( str / pos ) (if (setq pos (vl-string-position 44 str)) (cons (substr str 1 pos) (csv->lst (substr str (+ pos 2)))) (list str) ) ) (defun C:Readdat ( / size dlist fp newl a b c ) (setq size (strcat "*" (getstring "\nEnter Size <6, 7, 8 or 9>: "))) ;enter size req'd (setq file (findfile "test.dat")) ;find data file (setq fp (open file "r")) ;open file to read (setq newl (read-line fp)) ;first line is label for file (while (setq newl (read-line fp)) (if (= newl size) (progn (setq newl (read-line fp)) ; read the co-ords (setq lst '()) (setq lst (csv->lst newl)) ; Make a list of csv (set (read "a") (atof (nth 0 lst))) (set (read "b") (atof (nth 1 lst))) (set (read "c") (atof (nth 2 lst))) ) ) ) (close fp) (princ) ) This is test file *6 152.0,10.0,124.0 *7 178.0,12.0,135.0 *8 203.0,14.0,146.0 *9 229.0,16.0,158.0 Quote
AbdRF Posted March 12, 2020 Author Posted March 12, 2020 @BIGAL Thanks for your explanation.I understood now that it was for removing the comma to make it autolisp list . Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.