Add value if item in text is number

Add value if item in text is number

neam
Collaborator Collaborator
707 Views
14 Replies
Message 1 of 15

Add value if item in text is number

neam
Collaborator
Collaborator

Hi everyone

How to add a value to all number item in text string like this:

Old Text=Station: 10 No.=1

Value=1.5

New Text=Station: 11.5 No.=2.5

0 Likes
Accepted solutions (1)
708 Views
14 Replies
Replies (14)
Message 2 of 15

komondormrex
Mentor
Mentor

@neam 

hi there,

are there other strings with more than 2 numbers in a string, but to change only two?

0 Likes
Message 3 of 15

neam
Collaborator
Collaborator

Hi komondormrex

thank you for attention

Yes, there may be more than two numbers in the text.
Add a constant value to all numbers in the text.

0 Likes
Message 4 of 15

komondormrex
Mentor
Mentor

like what, i mean what chars are before/after the numbers?

0 Likes
Message 5 of 15

neam
Collaborator
Collaborator

I apologize if I didn't express my meaning correctly.
It may be after or before the number of any character.
Is it possible to identify numbers within a text?

For example:
Station:10  No.=1  Page 2

0 Likes
Message 6 of 15

neam
Collaborator
Collaborator

If it gets a little difficult, I can change the characters before and after the numbers to spaces.

Station 10 No. 1 Page 2

0 Likes
Message 7 of 15

ВeekeeCZ
Consultant
Consultant

FYI

Lees parse func can identify them correctly

https://www.lee-mac.com/parsenumbers.html

Message 8 of 15

komondormrex
Mentor
Mentor

so the page number stays 2 while station and no get added?

0 Likes
Message 9 of 15

neam
Collaborator
Collaborator

Hi beekeeCZ

Thank you for attention

I use this function in lisp, but i cannot replace new value to text string. 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;;
(defun incarray:splitstring ( str / lst )
(setq lst (vl-string->list str))
(read (vl-list->string (vl-list* 40 34 (incarray:split lst (< 47 (car lst) 58)))))
)
(defun incarray:split ( lst flg )
(cond
( (null lst) '(34 41))
( (member (car lst) '(34 92))
(if flg
(vl-list* 34 32 34 92 (car lst) (incarray:split (cdr lst) nil))
(vl-list* 92 (car lst) (incarray:split (cdr lst) flg))
)
)
( (or (< 47 (car lst) 58) (and (= 46 (car lst)) flg (< 47 (cadr lst) 58)))
(if flg
(vl-list* (car lst) (incarray:split (cdr lst) flg))
(vl-list* 34 32 34 (car lst) (incarray:split (cdr lst) t))
)
)
( flg (vl-list* 34 32 34 (car lst) (incarray:split (cdr lst) nil)))
( (vl-list* (car lst) (incarray:split (cdr lst) nil)))
)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Parse Numbers - Lee Mac
;; Parses a list of numerical values from a supplied string.
(defun LM:parsenumbers ( str )
( (lambda ( l )
(read
(strcat "("
(vl-list->string
(mapcar
'(lambda ( a b c )
(if (or (< 47 b 58)
(and (= 45 b) (< 47 c 58) (not (< 47 a 58)))
(and (= 46 b) (< 47 a 58) (< 47 c 58))
)
b 32
)
)
(cons nil l) l (append (cdr l) '(()))
)
)
")"
)
)
)
(vl-string->list str)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(setq num (getreal "Add Value: "))
(setq SS (ssget (list (cons 0 "TEXT,MTEXT"))))

(repeat (setq CNT (sslength SS))
(setq ENTITY (entget (ssname SS (setq CNT (1- CNT)))))
(setq TXTSTRING (strcase (cdr (assoc 1 ENTITY)) NIL))
(setq par (LM:parsenumbers TXTSTRING))
(setq x1 ())
(while (/= (car par) nil)
(setq x1 (reverse (append (list (rtos (+ num (car par)) 2 2)) x1)))
(setq par (cdr par))
)

(setq x2 (incarray:splitstring TXTSTRING))

0 Likes
Message 10 of 15

ВeekeeCZ
Consultant
Consultant

That's just a starting point, it needs to be adjusted. Specifically to replace READ with something which keeps it as strings..

 

Anyhow, where is his incarray:splitstring coming from?

 

 

Message 11 of 15

neam
Collaborator
Collaborator

That's absolutely correct.
I don't know exactly which forum I found it on.
I think it was from the Lee Mac.

0 Likes
Message 12 of 15

neam
Collaborator
Collaborator

I apologize for the late reply.
Due to my poor English, I have to use a translator, which slows down the work.

no: all number included Station and No. and Page are change.

I understand that the page cannot be decimal.

it is only an example:

Old= Station 10 No. 1 Page 2

New= Station 11.5 No. 2.5 Page 3.5

0 Likes
Message 13 of 15

komondormrex
Mentor
Mentor

so confirm if i am right.

you want every number that is in the textstring of all selected *texts to be changed by adding some number, and there will be no other number in the string to stay intact, correct?

0 Likes
Message 14 of 15

neam
Collaborator
Collaborator

Absolutely Yessssssssssssssssssssssssssssssss

👌👌

0 Likes
Message 15 of 15

komondormrex
Mentor
Mentor
Accepted solution

well, check the following. i did thou sort of filtering for words preceding numbers to be added, just in case)

;*****************************************************************************************************************
;
;	'add_value_texts' custom command adds value to certain numbers within text strings
;	komondormrex, feb, 2025
;	totally lm free
;
;*****************************************************************************************************************

(defun string_to_list (string delimiter_string / out_list)
    (setq char_list (vl-string->list string))
  (defun parse_string (char_list delimiter_string / substring)
    (setq char_list_remainder char_list)
    (if (vl-some '(lambda (ascii_code)
              (setq substring (cons ascii_code substring)
                  char_list_remainder (cdr char_list_remainder)
            )
              (member ascii_code (vl-string->list delimiter_string))
              )
              char_list
      )
      (progn
        (setq delimiter_char (chr (car substring)))
          (if (/= "" (setq substring (vl-list->string (reverse (cdr substring)))))
          (setq out_list (append out_list (list substring delimiter_char)))
          (setq out_list (append out_list (list delimiter_char)))
        )
        (parse_string (setq char_list char_list_remainder) delimiter_string)
      )
      (if (/= "" (setq substring (vl-list->string (reverse substring))))
        (setq out_list (append out_list (list substring)))
      )
     )
     out_list
  )
  (parse_string char_list delimiter_string)
)

;*****************************************************************************************************************

(defun check_int (number)
  (if (zerop (abs (rem number 1))) (itoa (fix number)) (rtos number))
)

;*****************************************************************************************************************

(defun c:add_value_texts (/ addition text_sset new_text_list do_add old_text_group)
  (if (null addition_saved) (setq addition_saved 1.5)) 
  (if (null (setq addition (getreal (strcat "\nEnter addition number <" (rtos addition_saved) ">: "))))
	(setq addition addition_saved)
	(setq addition_saved addition)
  )
  (if (setq text_sset (ssget '((0 . "*text") (1 . "*#*"))))
    (foreach text (vl-remove-if 'listp (mapcar 'cadr (ssnamex text_sset)))
      (setq new_text_list nil do_add nil)
      (foreach sub_string (string_to_list (cdr (setq old_text_group (assoc 1 (entget text)))) " :=")
        (if (and (wcmatch (strcase sub_string) (strcase "Station*,No.*,Page*"))
                 (null do_add)
            )
            (setq do_add t)
        )
        (if (and (vl-every '(lambda (asc) (member asc (vl-string->list "0123456789.")))
                   	    (vl-string->list sub_string)
                 )
                 do_add
            )
            (setq new_text_list (append new_text_list (list (check_int (+ (atof sub_string) addition))))
            	  do_add nil
            )
            (setq new_text_list (append new_text_list (list sub_string)))
        )
      )
      (entmod (subst (cons 1 (apply 'strcat new_text_list)) old_text_group (entget text)))
    )
  )
  (princ)
)

;*****************************************************************************************************************