Find every "=" character in text

Find every "=" character in text

neam
Collaborator Collaborator
873 Views
9 Replies
Message 1 of 10

Find every "=" character in text

neam
Collaborator
Collaborator

Hi

I tried changing the number after the "=" character.
But when there are two or more numbers of "=" in the text, it does not work correctly.

Please guide me and modify my code.

0 Likes
Accepted solutions (1)
874 Views
9 Replies
Replies (9)
Message 2 of 10

Moshe-A
Mentor
Mentor

@neam  hi,

 

Here is a nice function to return the positions of a character in a string.

 

usage:

(get-char-pos "L=0.020, R=-0.020" "=") => (1 10)

 

enjoy

Moshe

 

 

(defun get-char-pos (str ch / p) 
 (setq p -1)
 (vl-remove-if
  'not
  (mapcar
   (function
    (lambda (n)
     (setq p (1+ p))
     (if (= n (ascii ch)) p)
    )
   )
   (vl-string->list str) 
  )
 )
)

 

 

Message 3 of 10

Kent1Cooper
Consultant
Consultant

It is not clear to me what your sample drawing is intended to show.  Is the red-X part what you are starting with, or is it an incorrect result?  If it's an incorrect result, what are you starting with?  If it's two starting Text objects, and you want the green-check result above from the upper yellow one, it implies that both R&L values are the same, but they are not [one is negative, one positive].  If the lower yellow one is supposed to turn into the bottom green-checked pair, where are the 7's coming from?

 

I would appreciate a clearly-distinguished before-vs.-after sample drawing or image.

Kent Cooper, AIA
0 Likes
Message 4 of 10

komondormrex
Mentor
Mentor

before - [in/de]crementing numbers; after - percentage of those, imo.

deviations to be precise.

Message 5 of 10

pbejse
Mentor
Mentor

All 5 Text object on the drawing are showing its  current string value, and not a before and after.

 

Only 3 of these text objects produces the desired result when running the attached F&R.lsp, those with the check mark.

Those inside the red box result will not give the correct result as it only process the first value after the "=" symbol

[ Desired result ]

R&L= - 2.00%

L= + 2.00%, R=-2.00%

L= - 2.00%, R=+2.00%

R= + 2.70%

L= -2.70%

 

Message 6 of 10

neam
Collaborator
Collaborator

It was a very useful tip, I will try to use it

0 Likes
Message 7 of 10

neam
Collaborator
Collaborator

I absolutely mean that you get it

0 Likes
Message 8 of 10

neam
Collaborator
Collaborator

Dear Kent1

I am sorry that I could not express my meaning correctly. Please see pbejse's message in this post.

0 Likes
Message 9 of 10

pbejse
Mentor
Mentor
Accepted solution
(defun c:dev (/		    ss		  pos		i
	      stringValue   firstPart	  secondPart	newNumberValue
	      newStringValue		  entityData
	     )
  (if (setq ss (ssget "_:L"
		      '((0 . "TEXT") (1 . "*=*")
			(-4 . "<NOT") (1 . "*=*%")(-4 . "NOT>")
		       )
	       )
      )
    (repeat (setq i (sslength ss))
      (setq pos	0
	    entityData
	     (entget (ssname ss (Setq i (1- i))))
      )
      (setq stringValue (cdr (assoc 1 entityData)))
      (while (setq pos (vl-string-search "=" stringValue (1+ pos)))

	(setq firstPart	 (substr stringValue 1 (+ pos 1))
	      secondPart (substr stringValue (+ pos 2))
	)

	(setq newNumberValue (rtos (* (atof secondPart) 100) 2 2))
	(setq newStringValue
	       (strcat firstPart
		       (if (minusp (read newNumberValue)) "" "+" )
		       newNumberValue "%"
		       (vl-string-left-trim "+-0123456789." secondPart
		       )
	       )
	)
	(setq entityData
	       (entmod (subst (cons 1 newStringValue)
			      (assoc 1 entityData)
			      entityData
		       )
	       )
	)
	(setq stringValue newStringValue)
      )
    )
  )
  (princ)
)

HTH

Message 10 of 10

CADaSchtroumpf
Advisor
Advisor

There was a challenge on a French forum recently that might be of service to you.
I invite you to consult it for other solutions...
My proposal was this:

(defun extractNumber (str / l rslt)
  (setq
    l
    (mapcar
      '(lambda (x)
        (if (and (> x 44) (< x 58) (/= x 47)) x 32)
      )
      (vl-string->list str)
    )
    l
    (mapcar
      '(lambda (x y)
        (if (not (= x y 32)) x)
      )
      l
      (append (cdr l) '(32))
    )
    l (vl-remove-if-not '(lambda (x) (eq (type x) 'INT) x) l)
    l (mapcar '(lambda (x) (if (not (eq x 32)) x (list nil))) l)
  )
  (eval (read (strcat "(setq rslt (list " (apply 'strcat (mapcar '(lambda (x) (if (not (listp x)) (chr x) " ")) l)) "))")))
)

 

Exemple:(extractNumber "L=0.020, R=-0.020") -> (0.02 -0.02)

Can this can help you?