KRBeckman Posted February 19, 2010 Posted February 19, 2010 Hey All, I'm trying to create a lisp routine that will prompt the user for a dimension precision that will allow the user to use the previous precision entry only if one exsits. Here's what I've got so far: (defun C:dd (/ old_osm) (setq old_clr (getvar "clayer") old_osm (getvar "osmode")) (if (eq prs "") (progn (initget 1 "1 2 4 8 16 32") (setq prs (getkword "\nDimension Precision: "))) (progn (setq old_prs prs) (initget "1 2 4 8 16 32") (setq prs (getkword (strcat "\nDimension Precision <" old_prs ">: "))))) (princ)) But its not working. Any ideas? Quote
lpseifert Posted February 19, 2010 Posted February 19, 2010 Try replacing (if (eq prs "") with (if (= prs nil) Quote
KRBeckman Posted February 19, 2010 Author Posted February 19, 2010 Wow, that's upsetting... lol, thanks a ton. Quote
lpseifert Posted February 19, 2010 Posted February 19, 2010 Actually I think (eq prs nil) would work, but the results from eq can be squirrely; I'm sure it has it's purpose, but I try to avoid it. Quote
KRBeckman Posted February 19, 2010 Author Posted February 19, 2010 Gotcha, another quick question... I've changed it a little to include a default value when prs is equal to nil, and now I just realized another issue... it accepts "3" as an input for "32". Is it possible to stop this, I would like it to allow only "32". Here's what I got: (defun C:dd (/ old_osm old_osm old_prs) (setq old_clr (getvar "clayer") old_osm (getvar "osmode")) (if (= prs nil) (progn (setq old_prs "4") (initget "1 2 4 8 16 32") (setq prs (getkword (strcat "\nDimension Precision <" old_prs ">: ")))) (progn (setq old_prs prs) (initget "1 2 4 8 16 32") (setq prs (getkword (strcat "\nDimension Precision <" old_prs ">: "))))) (princ)) Quote
alanjt Posted February 19, 2010 Posted February 19, 2010 Gotcha, another quick question... I've changed it a little to include a default value when prs is equal to nil, and now I just realized another issue... it accepts "3" as an input for "32". Is it possible to stop this, I would like it to allow only "32". Here's what I got: (defun C:dd (/ old_osm old_osm old_prs) (setq old_clr (getvar "clayer") old_osm (getvar "osmode")) (if (= prs nil) (progn (setq old_prs "4") (initget "1 2 4 8 16 32") (setq prs (getkword (strcat "\nDimension Precision <" old_prs ">: ")))) (progn (setq old_prs prs) (initget "1 2 4 8 16 32") (setq prs (getkword (strcat "\nDimension Precision <" old_prs ">: "))))) (princ)) Try something like (while (not (member prs '("1" "2".... Quote
Lee Mac Posted February 19, 2010 Posted February 19, 2010 I would approach it like this: (defun c:test nil (or prs (setq prs "4")) (initget "1 2 4 8 16 32") (setq prs (cond ((getkword (strcat "\nDimension Precision <" prs "> : "))) (prs))) ) Bear in mind that if the user enters '3' the getkword will still return 32. Quote
alanjt Posted February 19, 2010 Posted February 19, 2010 Bear in mind that if the user enters '3' the getkword will still return 32. Hence my above statement. BTW, I agree with you. Quote
KRBeckman Posted July 29, 2010 Author Posted July 29, 2010 I know this is a old one, but I have a related question that I can't figure out... Here's the code I have now: (while (not (and (= (strlen rvl) 1) (>= (ascii rvl) (ascii "A")) (<= (ascii rvl) (ascii "Z")))) (setq rvl (strcase (cond ((getstring "\nEnter Revision <A>:")) (T "A"))))) What I want it to do is prompt the user for a 1 letter uppercase string and keep asking until this is fufilled and also allow the user to press enter to accept the default "A". But when I run this code and try to press the enter button to accept the default, it loops and asks me again. Any help would be greatly appriciated. Quote
alanjt Posted July 29, 2010 Posted July 29, 2010 (defun _RevNo (/ str) (while (not (< 64 (ascii (setq str (strcase (cond (((lambda (/ s) (if (/= "" (setq s (substr (getstring "\nSpecify revision <A>: ") 1 1 ) ) ) s ) ) ) ) ("A") ) ) ) ) 91 ) ) ) str ) Quote
KRBeckman Posted July 29, 2010 Author Posted July 29, 2010 Would you mind explaining why mine didn't work and why your's does? Quote
alanjt Posted July 29, 2010 Posted July 29, 2010 Actually, since you only want A-Z, this should work just fine: (initget 0 ((lambda (i s) (while (< i 91) (setq s (strcat s (chr (setq i (1+ i))) " ")))) 64 "")) (setq rev (cond ((getkword "\nSpecify revision <A>: ")) ("A") ) ) Quote
KRBeckman Posted July 29, 2010 Author Posted July 29, 2010 Ah, I see it now... never thought to use lambda to create a list of acceptable values for a getkword... I still don't get why mine didn't work tho:? Quote
alanjt Posted July 29, 2010 Posted July 29, 2010 Ah, I see it now... never thought to use lambda to create a list of acceptable values for a getkword... I still don't get why mine didn't work tho:? You were trying to validate a nil statement as a string. You have set the rvl variable first. You were also going about your cond wrong since getstring will return "" if the user hits enter (see my code). Instead of using a function to create the string, you could have just typed "A B C..." Quote
KRBeckman Posted July 29, 2010 Author Posted July 29, 2010 (edited) Yeah, I just figured it'd be better coding to have a function... Also, when I try this one, it doesn't accept any input but pressing enter: (initget 0 ((lambda (i s) (while (< i 91) (setq s (strcat s (chr (setq i (1+ i))) " ")))) 64 "")) (setq rev (cond ((getkword "\nSpecify revision <A>: ")) ("A") ) ) But I was able to figure out my method... (while (not (and rvl (= (strlen rvl) 1) (>= (ascii "Z") (ascii rvl) (ascii "A")))) (if (= "" (setq rvl (strcase (getstring "\nEnter Revision <A>: ")))) (setq rvl "A"))) Edited July 29, 2010 by KRBeckman Updated code to fix bug Quote
alanjt Posted July 29, 2010 Posted July 29, 2010 Yeah, I just figured it'd be better coding to have a function... Also, when I try this one, it doesn't accept any input but pressing enter: (initget 0 ((lambda (i s) (while (< i 91) (setq s (strcat s (chr (setq i (1+ i))) " ")))) 64 "")) (setq rev (cond ((getkword "\nSpecify revision <A>: ")) ("A") ) ) Yes it does. That's what the cond statement is for. Same as what you were trying to replicate with getstring. Quote
KRBeckman Posted July 29, 2010 Author Posted July 29, 2010 Yes it does. That's what the cond statement is for. Same as what you were trying to replicate with getstring. Maybe I'm doing something wrong, but when I run this and try to input "A" for the prompt I get "Invalid option keyword." and it prompts me again, and continues to do it unitl i just press enter. Quote
alanjt Posted July 29, 2010 Posted July 29, 2010 Maybe I'm doing something wrong, but when I run this and try to input "A" for the prompt I get "Invalid option keyword." and it prompts me again, and continues to do it unitl i just press enter. Do you have this in code or are you just pasting it to the command line? If the latter, then you have to wrap it in a progn function or something of the like. (defun foo (/ rev) (initget 0 ((lambda (i s) (while (< i 91) (setq s (strcat s (chr (setq i (1+ i))) " ")))) 64 "")) (setq rev (cond ((getkword "\nSpecify revision <A>: ")) ("A") ) ) ) I didn't do it this way because I wanted to make sure you understood it wasn't required. Quote
KRBeckman Posted July 29, 2010 Author Posted July 29, 2010 Do you have this in code or are you just pasting it to the command line? If the latter, then you have to wrap it in a progn function or something of the like. (defun foo (/ rev) (initget 0 ((lambda (i s) (while (< i 91) (setq s (strcat s (chr (setq i (1+ i))) " ")))) 64 "")) (setq rev (cond ((getkword "\nSpecify revision <A>: ")) ("A") ) ) ) I didn't do it this way because I wanted to make sure you understood it wasn't required. There we go.... never knew that mattered. Thanks a ton for your help. Quote
alanjt Posted July 29, 2010 Posted July 29, 2010 There we go.... never knew that mattered. Thanks a ton for your help. You're welcome. 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.