jonathann3891 Posted August 23, 2022 Posted August 23, 2022 Is it possible to read-line of a text file after a comma? For example: Line 1 will have: TEST1,TOS %%script(Q = (%ObjectTopLevel): If Q>0 then appendstring ("+" & (FormatNumber(Q,3))) else appendstring (FormatNumber(Q,3))) But the list box will only show: TOS %%script(Q = (%ObjectTopLevel): If Q>0 then appendstring ("+" & (FormatNumber(Q,3))) else appendstring (FormatNumber(Q,3))) My current code segment to read a file: (setq LC nil) (setq OpenFile (open (findfile "LabelContent.cfg")"R")) (while (setq line (read-line OpenFile)) (setq LC (cons line LC)) );while (setq LC (reverse LC)) (close OpenFile) (dcl-ListBox-AddList LabelContent/Form1/ListBox1 LC) Quote
TemporaryCAD Posted August 23, 2022 Posted August 23, 2022 ;Takes a string, substring, and offset ;Will trim the line to everything BEFORE the substing. The offset edits directly (i.e. -1 = one less char, 1 = one more char) ;If nothing is found, returns original string (defun trimbefore (str sstr offset / cut rstr) (setq cut (vl-string-search sstr str) rstr str ) (if (/= cut nil) (setq rstr(substr str 1 (+ cut offset))) ) (princ rstr) ) ;Takes a string, substring, and offset ;Will trim the line to everything AFTER the substring. The offset edits directly (i.e. -1 = one less char, 1 = one more char) ;If nothing is found, returns original string (defun trimafter (str sstr offset / cut rstr) (setq cut (vl-string-search sstr str) rstr str ) (if (/= cut nil) (setq rstr(substr str (- cut offset))) ) (princ rstr) ) I wrote these for some file handling. It works with vl-string-search, so beware if there are multiple substring matches. Quote
Steven P Posted August 23, 2022 Posted August 23, 2022 Read the text after a comma. I like it when I can do many things with as few commands as i can. Makes remembering then easier. A file, text seperated by commas..... hmm similar to a comma separated valuies (CSV file)., in which case my second favourite thing, Lee Mac usually has a solution: http://lee-mac.com/readcsv.html I haven't checked to see if this will work for your sample though. This is longer than the code above but also includes for getting reading the file too. Might need a small adjustments to say look for .TXT file and not .CSV file perhaps. If you don't need that go with TemporaryCAD Quote
BIGAL Posted August 24, 2022 Posted August 24, 2022 Like Steven P you can change the delimiter in Lee's code. Just use correct number. (ascii (getstring)) enter the character and a number will appear. ; thanks to Lee-mac for this defun ; www.lee-mac.com ; 44 is comma 9 is tab 34 is space ; is 59 (defun _csv->lst ( str / pos ) (if (setq pos (vl-string-position 9 str)) (cons (substr str 1 pos) (_csv->lst (substr str (+ pos 2)))) (list str) ) ) 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.