jon123 Posted May 10, 2021 Posted May 10, 2021 Hi, I have a large number of strings that contain between 7 and 16 characters. Does someone have a lisp that will remove all but the last 3 or 4 characters. The 3 or 4 characters being defined by the user eg 3 character original "123 456 789" = "789" or 4 characters original "123 10.5 15 1234" = "1234" Thanks in advance for any assistance. Quote
ronjonp Posted May 11, 2021 Posted May 11, 2021 Give this a try: (defun c:foo (/ el s str) (if (setq s (ssget ":L" '((0 . "text") (1 . "* *")))) (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex s))) (setq str (cdr (assoc 1 (setq el (entget e))))) (entmod (append el (list (cons 1 (substr str (+ 2 (vl-string-position 32 str 0 t))))))) ) ) (princ) ) Quote
ten0s Posted May 20, 2021 Posted May 20, 2021 Hi, First define a function that returns a suffix of a given length from string (defun string-suffix (len str / start) (setq start (1+ (- (strlen str) len))) (if (<= start 0) (setq start 1)) (substr str start)) Test it > (string-suffix 0 "abcdefg") "" > (string-suffix 1 "abcdefg") "g" > (string-suffix 3 "abcdefg") "efg" > (string-suffix 100 "abcdefg") "abcdefg" Then use the function whatever you feel like (setq lists '("123 456 789" "123 10.5 15 1234")) Map over your strings > (mapcar '(lambda (str) (string-suffix 3 str)) lists) ("789" "234") Or print them > (foreach str lists (princ (strcat (string-suffix 3 str) "\n"))) 789 234 Quote
BIGAL Posted May 22, 2021 Posted May 22, 2021 (edited) Maybe something like this Mutli radio buttons.lsp (in Cadtutor Downloads) supports up to about 20 buttons screen dependant ie a string with 20 values. Just make a list of the items in a string, Happy to provide more info, would just pick a string (setq lst '("Choose to save" "123" "345" "567")) ; make this from a string (ah:butts but "V" lst) Edited May 22, 2021 by BIGAL Quote
jon123 Posted May 23, 2021 Author Posted May 23, 2021 Thanks for the reply's. I was unable to get the below to work. It does not appear to do anything after selecting the text. On 11/05/2021 at 10:48, ronjonp said: Give this a try: (defun c:foo (/ el s str) (if (setq s (ssget ":L" '((0 . "text") (1 . "* *")))) (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex s))) (setq str (cdr (assoc 1 (setq el (entget e))))) (entmod (append el (list (cons 1 (substr str (+ 2 (vl-string-position 32 str 0 t))))))) ) ) (princ) ) This level of information is great but also far beyond my skill level to interpret and use. My effective level with all this is about zero. On 21/05/2021 at 02:35, ten0s said: Hi, First define a function that returns a suffix of a given length from string (defun string-suffix (len str / start) (setq start (1+ (- (strlen str) len))) (if (<= start 0) (setq start 1)) (substr str start)) Test it > (string-suffix 0 "abcdefg") "" > (string-suffix 1 "abcdefg") "g" > (string-suffix 3 "abcdefg") "efg" > (string-suffix 100 "abcdefg") "abcdefg" Then use the function whatever you feel like (setq lists '("123 456 789" "123 10.5 15 1234")) Map over your strings > (mapcar '(lambda (str) (string-suffix 3 str)) lists) ("789" "234") Or print them > (foreach str lists (princ (strcat (string-suffix 3 str) "\n"))) 789 234 Thanks for the suggestion about the radio buttons. Unfortunately I need to be also to select to keep either 3 or 4 characters on the right side of the text as the numbers do not repeat within a drawing. There are area a different number of characterless in each drawing is I can just delete the first 6 characters and keep whats left. On 22/05/2021 at 10:11, BIGAL said: Maybe something like this Mutli radio buttons.lsp (in Cadtutor Downloads) supports up to about 20 buttons screen dependant ie a string with 20 values. Just make a list of the items in a string, Happy to provide more info, would just pick a string (setq lst '("Choose to save" "123" "345" "567")) ; make this from a string (ah:butts but "V" lst) Below is a small sample of the text: drawing 1. 201 0.5 14 123 201 1 13 124 201 0.7 11 158 201 0.5 10 162 201 1.2 18 114 Drawing 2. 201 0.5 14 2301 201 1 13 2485 201 0.7 11 2689 201 0.5 10 2547 201 1.2 18 2519 Thanks again for all the help so far. Quote
Stefan BMR Posted May 23, 2021 Posted May 23, 2021 (defun _last_str (str / n) (if (setq n (vl-string-position 32 str nil T)) (substr str (+ n 2)) str ) ) _$ (_LAST_STR "201 1 13 124") "124" _$ (_LAST_STR "201 0.5 14 2301") "2301" _$ Quote
ronjonp Posted May 24, 2021 Posted May 24, 2021 16 hours ago, jon123 said: Thanks for the reply's. I was unable to get the below to work. It does not appear to do anything after selecting the text. This level of information is great but also far beyond my skill level to interpret and use. My effective level with all this is about zero. Thanks for the suggestion about the radio buttons. Unfortunately I need to be also to select to keep either 3 or 4 characters on the right side of the text as the numbers do not repeat within a drawing. There are area a different number of characterless in each drawing is I can just delete the first 6 characters and keep whats left. Below is a small sample of the text: drawing 1. 201 0.5 14 123 201 1 13 124 201 0.7 11 158 201 0.5 10 162 201 1.2 18 114 Drawing 2. 201 0.5 14 2301 201 1 13 2485 201 0.7 11 2689 201 0.5 10 2547 201 1.2 18 2519 Thanks again for all the help so far. Strange .. works here. Remove all characters at the last space. Quote
BIGAL Posted May 25, 2021 Posted May 25, 2021 A bit late to the party. Post did not go through If you pick the string you want to keep it will not change text where string does not exist. So try this. Pick 18 as a test pick 201. (defun c:savetxt ( / ss txt txtlst ans obj but ) (setq ss (ssget '((0 . "TEXT")))) (setq txt (cdr (assoc 1 (entget (ssname ss 0))))) (setq txtlst (_csv->lst txt)) (setq txtlst (append (list "Please choose txt ") txtlst)) (if (not AH:Butts)(load "Multi Radio buttons.lsp")) (if (not but)(setq but 1)) (setq ans (ah:butts but "v" txtlst)) (repeat (setq x (sslength ss)) (setq obj (entget (ssname ss (setq x (- x 1))))) (setq txt (cdr (assoc 1 obj))) (setq txtlst (_csv->lst txt)) (if (= (nth (- but 1) txtlst) ans) (entmod (subst (cons 1 ans) (assoc 1 obj) obj)) ) ) (princ) ) Taking Stefans idea a bit further. (defun c:txtlast ( / ss txt txtlst obj) ; thanks to Lee-mac for this defun ; www.lee-mac.com ; 44 is comma ; 32 is space (defun _csv->lst ( str / pos ) (if (setq pos (vl-string-position 32 str)) (cons (substr str 1 pos) (_csv->lst (substr str (+ pos 2)))) (list str) ) ) (setq ss (ssget '((0 . "TEXT")))) (repeat (setq x (sslength ss)) (setq obj (entget (ssname ss (setq x (- x 1))))) (setq txt (cdr (assoc 1 obj))) (setq txtlst (_csv->lst txt)) (entmod (subst (cons 1 (last txtlst)) (assoc 1 obj) obj)) ) (princ) ) Multi radio buttons.lsp 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.