Jump to content

Getting Number of String inside a string


Ahmeds

Recommended Posts

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?

Link to comment
Share on other sites

  • Replies 21
  • Created
  • Last Reply

Top Posters In This Topic

  • Tharwat

    8

  • pBe

    8

  • Ahmeds

    5

  • Lee Mac

    1

Top Posters In This Topic

(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 by pBe
Link to comment
Share on other sites

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))
)

Link to comment
Share on other sites

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))
     )

Link to comment
Share on other sites

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 by Ahmeds
Link to comment
Share on other sites

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)
               )
           )
       )
   )
)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :lol:) 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.

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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 by pBe
Link to comment
Share on other sites

wrote the code on the fly is all...

 

That's very clear with the name :shock:

 

:lol: indeed Iharwat..

 

No offense , If the routine did not find any text it would return "bad argument type ...."

Link to comment
Share on other sites

Nicely done , the OP must be very satisfied with these pretty options now . :thumbsup:

 

I hope so, think i scared the OP off :lol:

 

Thank you Tharwat, you are too kind.

 

Cheers

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...