Allowing numbers to be input with specific characters

Allowing numbers to be input with specific characters

saitoib
Advocate Advocate
657 Views
7 Replies
Message 1 of 8

Allowing numbers to be input with specific characters

saitoib
Advocate
Advocate

Hi all

 

I found that I can limit the input elements with initget


As another case, is it possible to eyeball a real number and a limited character, say T, and allow input?
I want to allow input only for numbers 0 and 1.5 and T.


Thank you very much for your help.

Saitoib
0 Likes
Accepted solutions (1)
658 Views
7 Replies
Replies (7)
Message 2 of 8

dbroad
Mentor
Mentor

(initget 1 "1 1.5 T")
(setq val (getkword "\nEnter value [1/1.5/T] : ")) ;will return answer in string form.
(setq val2 (cdr (assoc val '(("1" . 1)("1.5" . 1.5) ("T" . "T")))));This form converts numbers and keeps T as a string.
(setq val3 (read val));This form converts strings to numbers or to symbols.

 

EDIT:  Note that the 1 in the initget list and the prompt should've been 0, but you could use the same system for any set of numbers, strings, or symbols.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 3 of 8

Kent1Cooper
Consultant
Consultant

@saitoib wrote:

....

I want to allow input only for numbers 0 and 1.5 and T.

....


Another way, which will return numbers directly for 0.0 and 1.5 and also allow T as text:

 

(while (not (member whatever '(0 1.5 "T")))

  (initget "T"); allows "T" as input even to (getreal) function

  (setq whatever (getreal "\nEnter 0, 1.5 or T only: "))

)

 

It will keep asking again if a number other than 0 or 1.5 or text other than "T" is entered, but no conversion is needed afterward for allowed inputs [unless you want 0 as an integer instead of 0.0].

Kent Cooper, AIA
0 Likes
Message 4 of 8

CodeDing
Advisor
Advisor

@saitoib ,

 

In addition to what these other great forum members have to say, it might be useful for you to know where the resources are to research this information yourself also:

 

(initget ...)

https://help.autodesk.com/view/OARX/2020/ENU/?guid=GUID-9ED8841B-5C1D-4B3F-9F3B-84A4408A6BBF

 

(getkword ...)

https://help.autodesk.com/view/OARX/2020/ENU/?guid=GUID-9F940144-0D7B-4DA1-BF50-BBF8FB8DFF21

 

(getreal ...)

https://help.autodesk.com/view/OARX/2020/ENU/?guid=GUID-503BBE86-9B22-456A-8883-1B5F5832B7E0

 

Hope that helps!

Best
~DD

0 Likes
Message 5 of 8

saitoib
Advocate
Advocate

I'm sorry, I didn't explain myself very well.
I want to allow input of any and all numbers and "T"

Saitoib
0 Likes
Message 6 of 8

Kent1Cooper
Consultant
Consultant
Accepted solution

@saitoib wrote:

....
I want to allow input of any and all numbers and "T"


Then all you need is this:

 

  (initget "T"); allows "T" as input even to (getreal) function

  (setq whatever (getreal "\nEnter number or T only: "))

Kent Cooper, AIA
Message 7 of 8

saitoib
Advocate
Advocate

@Kent1Cooper 

Okay.
So this was the original use of initget.
I learned a lot.
Thank you very much.

Saitoib
0 Likes
Message 8 of 8

dbroad
Mentor
Mentor

Based on your OP, limiting the input to 0, 1.5 or T, you selected an answer that didn't meet your original conditions.  Both post 2 and post 3 met those conditions but not the post marked as the solution.  You don't need to mark other solutions. I just thought I'd point that out for other future readers of the thread.

Architect, Registered NC, VA, SC, & GA.
0 Likes