Jump to content

Seperate number and text (with space between them).


Sheep

Recommended Posts

Hello all. Hope everybody doing fine during Covid19.

I tried using Getreal but but got an error "Requires numeric value."

For example: 123 <space> df. I want to seperate it so i can use it later. Tried using CAR and CADR to get the number and text.

Any idea without using VLX. Thanks

0.PNG

Link to comment
Share on other sites

You want an input that includes a space character?

(setq val (getstring T "\nEnter Value :"))

(setq p (vl-string-position 32 val))
(setq number  (substr val 1 p ))
(Setq therest (substr val (+ 2 p)))

 

If you want the variable number to be a real number use (distof number)

 

Link to comment
Share on other sites

1 hour ago, pBe said:

You want an input that includes a space character?

Thank you for your reply. Actually i want to remove the space and get the number and text.

For example, i want to enter a distance (numbers) and notes (text) in one line. 123<space>df. The number will get its own Setq and same for the text. Is it possible to do it without VLX?

Link to comment
Share on other sites

4 minutes ago, Sheep said:

Thank you for your reply. Actually i want to remove the space and get the number and text.

For example, i want to enter a distance (numbers) and notes (text) in one line. 123<space>df. The number will get its own Setq and same for the text. Is it possible to do it without VLX?

Sheep That is exactly what i posted,

Let me put it in a way you can use on command prompt

(defun c:demo (/ val p number text)
  (setq val (getstring T "\nEnter Value :"))
  (setq p (vl-string-position 32 val))
  (setq number (substr val 1 p))
  (Setq text (substr val (+ 2 p)))
  (princ (strcat "\nVariable number: " number))
  (princ (strcat "\nVariable text: " text))
  (princ)
)

Unless without VLX means something else entirely, then i'm fresh out of ideas

 

  • Like 1
Link to comment
Share on other sites

vl-string-position (AutoLISP) <-- link  1

ascii (AutoLISP)  < --- link 2

vl-string-search (AutoLISP) < --- link 3

 

Quote

 What is vl-string-position?

 

The functions return value is an integer representing the displacement at which char-code was found from the beginning of the string [ refer to link #1 ],

in this case  (vl-string-position 32 "123 df") = 3 

Where in this string is the character " " located  with this string value ? "123 df"

 

Quote

"But why 32?"

Because the vl-string-position is using a numeric value representation of the character to be searched. [ refer to link #1 ][[ refer to link #2]

 

You can however use another function which will give you the same result.

(Vl-string-search " " "123 df")  = 3

 

Quote

"Why would you use first one and not the other"

 

Thats the first thing that pop into my head, force of habit, I dont know. But serioulsy vl-string-position is reportedly faster than vl-string-search. 

 

There's also:

(setq str "123 df")
(vl-position 32 (VL-STRING->LIST str)) = 3
(vl-string-mismatch (itoa (atoi str)) str) = 3
( (lambda (n s / a )
	(while (not (eq (setq a (substr s 1 1)) " "))
	  	(setq s (substr s 2) n (1+ n)))
	    )
	 0 str 
      )

An why am I posting all this? I have nothing else better to do today 😄

 

  • Thanks 1
Link to comment
Share on other sites

2 minutes ago, pBe said:

 

An why am I posting all this? I have nothing else better to do today 😄

 

To be honest, this is the most complete answer i got. I don't mind if someone give out the keyword and i'll search it later but this....just great. Thank you for your time. A great leader always explain so other can follow.

Link to comment
Share on other sites

4 hours ago, Sheep said:

 

For example: 123 <space> df. I want to seperate it so i can use it later. Tried using CAR and CADR to get the number and text.

 

 

car & cadr ?

 

hint:

(setq str "123 DF" 
      lst (read (strcat "(" str ")" )) )
      (print (cadr lst))

$0.02

(defun c:tt ( / lst )
  (or
    (and
      (setq lst (read (strcat "(" (getstring t "\nInput value : ") ")"))) ;evaluate as LIST
      (mapcar ''( (a b) (princ  (strcat "\n" a (vl-princ-to-string b)))) 
              '( "Number : " "Text : ") 
              lst
      )
    )
      (princ "\nNull input?")
  )
  (princ)
)

 

 

 

 

  • Thanks 1
Link to comment
Share on other sites

1 minute ago, hanhphuc said:

(read (strcat "(" (getstring t "\nInput value : ") ")")))
 ...  (strcat "\n" a (vl-princ-to-string b)))) 
              '( "Number : " "Text : ") 
..

 

 

Yeah, that too. :)

 

 

 

Link to comment
Share on other sites

My $0.05 no Vl you can do a repeat and look at each character in a string, Strlen,  (substr str x 1) using strcat to join together once a space is found its numb then continue to end of string to get text part. Its probably the slowest way. Do this way for MAC no VL.

Link to comment
Share on other sites

On 6/19/2020 at 2:56 PM, pBe said:

Sheep That is exactly what i posted,

Let me put it in a way you can use on command prompt


(defun c:demo (/ val p number text)
  (setq val (getstring T "\nEnter Value :"))
  (setq p (vl-string-position 32 val))
  (setq number (substr val 1 p))
  (Setq text (substr val (+ 2 p)))
  (princ (strcat "\nVariable number: " number))
  (princ (strcat "\nVariable text: " text))
  (princ)
)

Unless without VLX means something else entirely, then i'm fresh out of ideas

 

Hi again. I tried you sample and when i didn't enter the text, it wil give out numberp:nil. Maybe i didn't give out the full requirement sorry. The distance is a MUST but the text not all the time. 

Example :

123 dx

123

1234 df

1234

Can i use IF (= nil) for the text?

I hope my English can give you an idea.

 

Edited by Sheep
Link to comment
Share on other sites

1 hour ago, Sheep said:

Can i use IF (= nil) for the text?..

 

Yes,. very good, that is the general idea,  include a condtional function

(Setq text  (if p (substr val (+ 2 p))))

That will result to nil, then when the time to use the text variable

(if text ... do something)

or

(Setq text  (if p (substr val (+ 2 p)) "")) '<-- leave text variable as blank
(Setq text  (if p (substr val (+ 2 p)) "Not included)) '<-- or use a defualt value

That way, when you include the text variable in a statement. it will still have a value  and theres no need for a test 

(princ (strcat "\nPart number: " number "\nDescription: " text))

and so...

(defun c:demo (/ val p number text)
   (setq val (getstring T "\nEnter Value :"))
   (setq p (vl-string-position 32 val))
   (setq number (substr val 1 p))
   (Setq text  (if p (substr val (+ 2 p)) "Not Included"))
      
   (princ (strcat "\nPart number: " number "\nDescription: " text))   

  (princ)
)

Command: DEMO

Enter Value :123 dx

Part number: 123
Description: dx

 

Command: DEMO

Enter Value :123

Part number: 123
Description: Not Included

 

Command: DEMO

Enter Value :1234 df

Part number: 1234
Description: df

 

Command: DEMO

Enter Value :1234

Part number: 1234
Description: Not Included

 

HTH

  • Thanks 1
Link to comment
Share on other sites

On 6/19/2020 at 4:03 PM, hanhphuc said:

 


(defun c:tt ( / lst )
  (or
    (and
      (setq lst (read (strcat "(" (getstring t "\nInput value : ") ")"))) ;evaluate as LIST
      (mapcar ''( (a b) (princ  (strcat "\n" a (vl-princ-to-string b)))) 
              '( "Number : " "Text : ") 
              lst
      )
    )
      (princ "\nNull input?")
  )
  (princ)
)

By using your example i can extract the list (lst) using (nth 0 lst) and (nth 1 lst). Now i dont have to worry about the space thanks @hanhphuc.

 

 

 

 

Edited by Sheep
Link to comment
Share on other sites

22 minutes ago, Sheep said:

By using your example i can extract the list (lst) using (nth 0 lst) and (nth 1 lst). 

 

please be careful, LIST = 'space delimited'

"123 DF ABC" --> ("123" "DF" "ABC") ; 3 items in list

suggestion, underscore "_" or dash "-" 

"123 DF_ABC" ->  ("123" "DF_ABC") ; 2 items in list

 

 

  • Like 1
Link to comment
Share on other sites

For this "123 DF ABC" go back to maybe substr as soon as a space its not a number then whatever is left is a string. "123 DF ABC don't forget efg"

 

(123 "DF ABC don't forget efg") ;convert number atof etc

 

"123 DF ABC don't forget efg 54"

(123 "DF ABC don't forget efg 54")

Edited by BIGAL
  • Thanks 1
Link to comment
Share on other sites

8 hours ago, BIGAL said:

For this "123 DF ABC" go back to maybe substr as soon as a space its not a number then whatever is left is a string. "123 DF ABC don't forget efg"

 

 

yes, agree dealing with STRING, preferable eg: substr, strlen, vl-string- functions  etc..

 

eval list hiccups = strcase all capital & blank ignored

(defun c:tt (/ lst n $)
  (or
    (and
      (setq lst (read (strcat "(" (getstring t "\nInput value : ") ")")))
      (setq n (length lst)) 
      (mapcar '(lambda (a b) (setq $ (vl-princ-to-string b))
                 (princ (strcat "\n" a
                          (cond
                            ((and (listp b) (> n 2)) (substr $ 2 (- (strlen $) 2)))
                            (t $)
                          )
                        )
                 )
               )
              '( "Number : " "Text : ")
              (if (> n 2) (list (car lst) (cdr lst)) lst)
      )
    )
    (princ "\nNull input?")
  )
  (princ)
)

 


command: TT
Input value : 123 df abc

Number : 123
Text : DF ABC

 



 

 

 

 

Edited by hanhphuc
duplicate vl-princ
  • Thanks 1
Link to comment
Share on other sites

Hi all,

Just hit another roadblock with autolisp. How to i convert those numbers into a coordinate (list?) X,Y,z(0).

Example:

123 (- UserInput aPLow)

456 (i make a CADR from a GETPOINT)

0 (z value stay at 0)

I want to get (123 456 0) so i can create a line at that point.

Tried 

(setq xyzCombo (list Text1 (cadr aPLow) 0 ))

Whenever i run the lisp, my Acad will stop running.

0.PNG

Edited by Sheep
Added an image.
Link to comment
Share on other sites

I think your trying to hard just use my multi getvals get X Y Z much simpler.

 

(setq lst (list x y z))

 

Or just use X,Y,Z this is already done Lee-mac  => ( X Y Z)

Edited by BIGAL
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...