Ahmeds Posted December 15, 2012 Share Posted December 15, 2012 If I have a layer named (ex. boundary), and this this layer has only one content. For example: A=45,000 SQ.M. or A=120,000 SQ.M. My question is how could I get that "45,000" or "120,000" which is placed at the middle of the string? Quote Link to comment Share on other sites More sharing options...
pBe Posted December 15, 2012 Share Posted December 15, 2012 (edited) (defun _extract (str) (vl-list->string (vl-remove-if-not '(lambda (x) (or (< 47 x 58) (= x 44))) (vl-string->list str)))) (defun _extract (str / a b) (while (and (setq a (substr str 1 1)) (not (eq a ""))) (if (or (< 47 (ascii a) 58) (= (ascii a) 44)) (setq b (strcat (if b b "") a) str (substr str 2)) (setq str (substr str 2)))) b) Edited December 15, 2012 by pBe Quote Link to comment Share on other sites More sharing options...
Tharwat Posted December 15, 2012 Share Posted December 15, 2012 Another ... (defun PeelStrings (st / s) (if (eq (type st) 'STR) (foreach x (vl-string->list st) (if (member x '(47 48 49 50 51 52 53 54 55 56 57 58 44)) (setq s (cons x s)) ) ) ) (vl-list->string (reverse s)) ) Quote Link to comment Share on other sites More sharing options...
pBe Posted December 16, 2012 Share Posted December 16, 2012 Recursion (defun _extract (str b / a) (setq a (substr str 1 1)) (if (and (not (eq a ".")) (or (numberp (read a))(eq a ","))) (setq b (strcat b a) str (substr str 2)) (setq str (substr str 2))) (if (eq str "") b (_extract str b)) ) Quote Link to comment Share on other sites More sharing options...
Ahmeds Posted December 16, 2012 Author Share Posted December 16, 2012 (edited) to Tharwat or pBE, Can you give me an example how to use it in my layer "boundary" having this string ("A=45,000 SQ.M.)? Edited December 16, 2012 by Ahmeds Quote Link to comment Share on other sites More sharing options...
Tharwat Posted December 16, 2012 Share Posted December 16, 2012 Can you give me an example how to use it in my layer "boundary" having this string ("A=45,000 SQ.M.)? e.g. (peelstrings "A=45,000 SQ.M.") Quote Link to comment Share on other sites More sharing options...
Ahmeds Posted December 16, 2012 Author Share Posted December 16, 2012 Thank you Tharwat. Quote Link to comment Share on other sites More sharing options...
Tharwat Posted December 16, 2012 Share Posted December 16, 2012 Thank you Tharwat. You're welcome Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted December 16, 2012 Share Posted December 16, 2012 If you require the numerical value for use in LISP: (defun parse ( s ) (read (vl-list->string (apply 'append (mapcar (function (lambda ( x ) (if (< 47 x 58) (list x))) ) (vl-string->list s) ) ) ) ) ) Quote Link to comment Share on other sites More sharing options...
Ahmeds Posted December 19, 2012 Author Share Posted December 19, 2012 No offense to you Lee because your code and Tharwat have almost the same output I need, but I prefer to use this: (peelstrings "A=45,000 SQ.M.") But I still have a follow up question for you guys, coz I tried it my own many times, I just can't get the exact code. I want to get the value(string) of that layer(e.g. "boundary") so that I don't have to write this (A=45,000 SQ.M.) every time I use the "PEELSTRINGS" command. I want it like this: (setq Bound <code.....>) (peelstrings "bound") This one should get the string exactly what the layer "boundary" has. Quote Link to comment Share on other sites More sharing options...
Tharwat Posted December 19, 2012 Share Posted December 19, 2012 If you assign the value of a string to a variable , you should feed it to the PeelStrings function as variable and not as a string , besides that the variable should be a string that holds numbers to let the function get number values only . (setq Bound "Boundary 123") (peelstrings bound) and it should return "123" But if you want to get numbers only you can use my other function in this post Quote Link to comment Share on other sites More sharing options...
pBe Posted December 19, 2012 Share Posted December 19, 2012 Does that mean TEXT entities on "BOUND" layer? Also, what really matters here Ahmeds is what you're planning to do with the extracted value. The snippets posted above (which you totally ignored ) will work on certain conditions, in this case target-ting a specific separator "," Now in the event you will be needing the result as a numerical value. parsing the string to include the "," separator will do you no good at all. The example you posted , which i assumed as AREA values A=120,000 SQ.M A=45,000 SQ.M. "120,000" "45,000" As we are still in the dark as to what the eventual use of these values for you. Now thats where LM's code will come to play. "120000" "45000" And me thinks you'll be better off using his code. Quote Link to comment Share on other sites More sharing options...
Ahmeds Posted December 19, 2012 Author Share Posted December 19, 2012 Does that mean TEXT entities on "BOUND" layer? Yes, I have one text entity in my "BOUNDARY" layer which is (e.g. A=45,000 SQ.M.).. No, I'm not ignoring your codes, I ask once to post how to work with your posted codes because I don't know how to use it, but you don't replied anymore.... to Tharwat or pBE,Can you give me an example how to use it in my layer "boundary" having this string ("A=45,000 SQ.M.)? Your codes doesn't runs to me perfectly, it only displays "too few arguments" Tharwat reply, So I used his codes instead and making my own way work fine for me by adding this. (defun add-comma (txt / strl cont1 lth cont txt1) (setq strl (strlen txt) cont1 1 txt1 "") (while (and (/= (substr txt cont1 1) ".") (<= cont1 strl)) (setq cont1 (1+ cont1))) (setq lth (1- cont1) cont1 1 cont (1- lth)) (if (> lth 3) (progn (while (< cont1 lth) (setq let (substr txt cont1 1) txt1 (strcat txt1 let)) (if (and (zerop (rem cont 3)) (eq (type (read let)) 'INT)) (setq txt1 (strcat txt1 ","))) (setq cont (1- cont) cont1 (1+ cont1))) (while (<= cont1 strl) (setq txt1 (strcat txt1 (substr txt cont1 1)) cont1 (1+ cont1))) txt1) txt)) SORRY.... If you'd think I ignored you, maybe I just can't wait longer for your reply to my post #5. Anyway, if ever you have codes to get that text entity from my "BOUNDARY" layer so I can automatically parse it.. so kind of you to forgive... Thanks.. Quote Link to comment Share on other sites More sharing options...
pBe Posted December 19, 2012 Share Posted December 19, 2012 (edited) I meant that as a joke, pay no mind to it dude i expect you know what to do with this (defun c:test ( / _extract ss i)(vl-load-com) (defun _extract (str) (vl-list->string (vl-remove-if-not '(lambda (x) (or (< 47 x 58) (= x 44))) (vl-string->list str)))) (if (setq ss (ssget "_X" '((0 . "TEXT") (8 . "Boundary") (1 . "*#`,#*")))) (repeat (setq i (sslength ss)) (print (_extract (cdr (assoc 1 (entget (ssname ss (setq i (1- i)))))))) ) (princ "\nString not found") ) (princ) ) HTH Edited December 19, 2012 by pBe Quote Link to comment Share on other sites More sharing options...
Tharwat Posted December 19, 2012 Share Posted December 19, 2012 Localizing variables is a very good habit . Quote Link to comment Share on other sites More sharing options...
pBe Posted December 19, 2012 Share Posted December 19, 2012 Localizing variables is a very good habit . indeed Iharwat.. wrote the code on the fly is all... Quote Link to comment Share on other sites More sharing options...
Tharwat Posted December 19, 2012 Share Posted December 19, 2012 wrote the code on the fly is all... That's very clear with the name indeed Iharwat.. No offense , If the routine did not find any text it would return "bad argument type ...." Quote Link to comment Share on other sites More sharing options...
pBe Posted December 19, 2012 Share Posted December 19, 2012 No offense , If the routine did not find any text it would return "bad argument type ...." Ooops code updated Quote Link to comment Share on other sites More sharing options...
Tharwat Posted December 19, 2012 Share Posted December 19, 2012 Ooops code updated Nicely done , the OP must be very satisfied with these pretty options now . Quote Link to comment Share on other sites More sharing options...
pBe Posted December 19, 2012 Share Posted December 19, 2012 Nicely done , the OP must be very satisfied with these pretty options now . I hope so, think i scared the OP off Thank you Tharwat, you are too kind. Cheers Quote Link to comment Share on other sites More sharing options...
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.