Small Fish Posted September 19, 2010 Posted September 19, 2010 Hi could someone tweak my code. I reckon I am close. I would also like to make it work for when the user just types in the letter 'n' or 'N' for no and 'y' or 'Y' for yes. thanks (defun c:YesNo (/ ans) (initget "YES yes NO no") (setq ans (strcase (getstring "\nYes or No: ") (cond ((eq ans "No") (alert "\nYou selected 'No'") ) ((eq ans "Yes") (alert "\nYou selected 'Yes'") ) );cond (princ) );defun Quote
lpseifert Posted September 19, 2010 Posted September 19, 2010 (defun c:YesNo (/ ans) (initget 1 "Yes No") (setq ans (getkword "\nYes or No: ")) (cond ((eq ans "No") (alert "\nYou selected 'No'") ) ((eq ans "Yes") (alert "\nYou selected 'Yes'") ) );cond (princ) );defun look in Developer's Help for getkword, the example there is almost identical to yours Quote
Small Fish Posted September 19, 2010 Author Posted September 19, 2010 thanks ipseifert so it was getword instead of getstring! I was following an example with getstring, but getword seems to be a simplier option cheers SF Quote
Lee Mac Posted September 19, 2010 Posted September 19, 2010 Some info I posted on here a while back: ;; Initget / GetK(ey)word Examples ;; © Lee Mac 2010 ;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= ;; Note 1: On Prompt String Format ;; In all examples, to achieve correct display ;; when DYNamic Input (DYNMODE=1) is enabled, ;; prompt string must be formatted thus: ;; [Option1/Option2/Option3] ;; Or, with default: ;; [Option1/Option2/Option3] <Option1> ;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= ;; Note 2: On Global Variable Format ;; In all examples utilising global variables ;; asterisks are included in variable names to decrease ;; risk of variable names clashing with unlocalised variables ;; from other programs. Asterisks are used for no other purpose. ;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= ;; 1. Force User Input (initget bit 1) (initget 1 "Alpha Beta Gamma") (setq ans (getkword "\nChoose [Alpha/Beta/Gamma]: ")) ;; User must select a choice to continue, else ;; Esc must be used to cancel evaluation ;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= ;; 2. Preset Default (Alpha) ;; a) (initget "Alpha Beta Gamma") (setq ans (cond ( (getkword "\nChoose [Alpha/Beta/Gamma] <Alpha>: ") ) ( "Alpha" ))) ;; b) (initget "Alpha Beta Gamma") (setq ans (getkword "\nChoose [Alpha/Beta/Gamma] <Alpha>: ")) (if (not ans) (setq ans "Alpha")) ;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= ;; 3. Dynamic Default (utlisation of Global Variable: *ans*) ;; a) (or *ans* (setq *ans* "Alpha")) (initget "Alpha Beta Gamma") (setq *ans* (cond ( (getkword (strcat "\nChoose [Alpha/Beta/Gamma] <" *ans* ">: ") ) ) ( *ans* ) ) ) ;; b) (if (not *ans*) (setq *ans* "Alpha")) (initget "Alpha Beta Gamma") (setq *ans* (cond ( (getkword (strcat "\nChoose [Alpha/Beta/Gamma] <" *ans* ">: ") ) ) ( *ans* ) ) ) ;; c) (initget "Alpha Beta Gamma") (setq *ans* (cond ( (getkword (strcat "\nChoose [Alpha/Beta/Gamma] <" (setq *ans* (cond ( *ans* ) ( "Alpha" )) ) ">: " ) ) ) ( *ans* ) ) ) Quote
Small Fish Posted September 20, 2010 Author Posted September 20, 2010 Thanks Lee - that will be useful as a 'how to' future reference SF Quote
irneb Posted September 20, 2010 Posted September 20, 2010 Firstly, initget does not work at all with getstring. You can only get exactly what the user typed using getstring, and if you add a (getstring T "....") the user needs to press Enter, enabling the string to even contain spaces. You can do something similar by checking the text entered after the getstring call. You can use wildcard matching and case conversion, e.g. if the user typed y or Y or yE or YO ..... or any combination a call to (wcmatch (strcase ans) "Y*") will return T ... as long as the text started with an upper- or lowercase Y it's fine. You could go further by checking only for letters starting with Y, the YE then YES: (wcmatch (strcase ans) "Y,YE,YES"). However, the user may still enter something strange like "Maybe" ... in which case the wcmatch line will return nil ... i.e. "No"??? Is this correct??? If not, you then need to ask the user again until they've typed something acceptable, in which case the getkword (together with initget) does all that for you. Most other get.... functions work with initget (to lesser or greater extent - see the developer help), even entsel, nentsel & nentselp work with at least the key-words. But if you only want to get some text input from the user, but need to force only some words, then the getKword (notice it's not getword, it's short for Get Key Word) is your best bet. Quote
Recommended Posts
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.