ESan Posted 20 hours ago Posted 20 hours ago (edited) After being stuck on this subject for a while, I know the answer is "don't use variables for the actions of an if statement" but I am still bothered as to WHY it does not work. I assume it has to do with the way the if function evaluates, but I cannot figure it out myself. I apologize in advance for the long read. For example, I created a short routine to give a positive or negative response dependent on whether or not the user input equals 1: (defun c:if (/ inp) (setq inp (getint "\nValue: ")) (if (= 1 inp) (print "You Did It!!!!!!!!!") (print "You FAILED") ) (princ) ) This routine works- I provide the action for the T or nil outcome within the if statement. However, if I assign variables instead: (defun c:if (/ inp yes no) (setq inp (getint "\nValue: ") yes (print "You Did It!!!!!") no (print "You FAILED") ) (if (= 1 inp) yes no) (princ) ) The routine doesn't know what to do with itself regardless of user input. It returns the nil outcome twice regardless of whether or not the user input value equals 1. I do not understand why it skips to the nil outcome if the statement is true. Edited 20 hours ago by SLW210 Added Code Tags!! Quote
SLW210 Posted 20 hours ago Posted 20 hours ago Please use Code Tags for your code in the future. (<> in the editor toolbar) Quote
pkenewell Posted 20 hours ago Posted 20 hours ago If you want to use an IF statement with a variable that could be something or NIL, simply use this construct: (defun C:IF () (setq i (getint "\nEnter a Number or ENTER for nothing: ")) (if i ;<-- If not nil (progn (princ (strcat "\nYou entered the number " (itoa i) ". ")) T ) (progn (Princ "\n You did not enter anything. ") nil ) ) ) Conversely if you want to check for a NIL condition: (defun C:IF () (setq i (getint "\nEnter a Number or ENTER for nothing: ")) (if (not i) ;<-- If nil (progn (Princ "\n You did not enter anything. ") nil ) (progn (princ (strcat "\nYou entered the number " (itoa i) ". ")) T ) ) ) Quote
rlx Posted 20 hours ago Posted 20 hours ago first of all , if is a built in command so c:if I don't think works second , yes and no are variables not functions so you could use (setq yes "\nYes") and then use (eval yes) better would be : (defun c:whatif (/ inp yes no) (setq yes "Yes" no "No") (if (= (setq inp (getint "\nValue: ")) 1)(eval yes)(eval no)) ;;; or (if (= (setq inp (getint "\nValue: ")) 1)(princ "\nyes")(princ "\nNo")) (princ) ) 1 Quote
ESan Posted 19 hours ago Author Posted 19 hours ago 38 minutes ago, SLW210 said: Please use Code Tags for your code in the future. (<> in the editor toolbar) I did not know that was a feature, sorry about that!! Quote
ESan Posted 19 hours ago Author Posted 19 hours ago 24 minutes ago, rlx said: first of all , if is a built in command so c:if I don't think works Ah, I wasn't 100% sure if it was or not. When I was testing this out, my command was just "OK". I thought I would change it to something more appropriate for clarity. 26 minutes ago, rlx said: yes and no are variables not functions so you could use (setq yes "\nYes") and then use (eval yes) Does the if function not automatically evaluate the T and nil outcomes? Using eval in place of just the variable does fix the issue when printing to the command line, however it does not work when asking it to perform an action, such as changing the users layer if inp does not equal1. If 1 is entered, it will show the correct evaluated option in the command line, but carry out the nil value regardless- changing the users layer as well. I might be trying to understand the if function in a situation where it is not realistically used. But I am not sure I know enough to know that either Quote
ESan Posted 19 hours ago Author Posted 19 hours ago 44 minutes ago, pkenewell said: (defun C:IF () (setq i (getint "\nEnter a Number or ENTER for nothing: ")) (if i ;<-- If not nil (progn (princ (strcat "\nYou entered the number " (itoa i) ". ")) T ) (progn (Princ "\n You did not enter anything. ") nil ) ) ) Conversely if you want to check for a NIL condition: Does the first set of code not automatically check for a nil option? From my understanding, the if function has a T and nil outcome and needs both to work. If the statement is true, carry out the first (T) option, if not, carry out the second (nil) option. Quote
pkenewell Posted 15 hours ago Posted 15 hours ago (edited) 4 hours ago, ESan said: Does the first set of code not automatically check for a nil option? From my understanding, the if function has a T and nil outcome and needs both to work. If the statement is true, carry out the first (T) option, if not, carry out the second (nil) option. @ESan I'm not sure what you mean exactly. The structure of an IF statement is: (IF <function or variable evaluates as True> <THEN perform statement or function and return value> <ELSE if not True, perform the statement or function and return value>) NOTE: the IF function doesn't return T or NIL by itself, it returns the results of the statement or function in the THEN or ELSE parts. Example with extra commenting: (defun C:IF () (setq i (getint "\nEnter a Number or ENTER for nothing: ")) (if i ;<-- IF "i" evaluates to not Nil or T (true) ;THEN perform the following. NOTE all must evaluate to a single return, hence why (progn) is used to do multiple actions. (progn (princ (strcat "\nYou entered the number " (itoa i) ". ")); Use i in print to command line T ; Return T - Otherwise (princ) doesn't return anything. ) ; ELSE if "i" evaluates to NIL, perform the following. NOTE all must evaluate to a single return, hence why (progn) is used to do multiple actions. (progn (Princ "\n You did not enter anything. "); Print to the command line that nothing was given. nil ; Return NIL - Otherwise (princ) doesn't return anything. ) ) ) Edited 15 hours ago by pkenewell Quote
mhupp Posted 8 hours ago Posted 8 hours ago This is another way of setting a default option from a prompt. (initget "Prefix Suffix") (setq rep (cond ((getkword "\nAdd Text @ [Prefix/<Suffix>]: ")) ("Suffix") ) ) This will prompt the user "Add Text @ [Prefix/<Suffix>]:" just visually using < > to mark the default answer. if the user hits enter on the prompt the kword isn't set so rep defaults to "Suffix" Using cond instead of if's & wrapping it with getkword means the user doesn't have to type out exactly Prefix or Suffix . to then trigger other if's later in the lisp. Quote
BIGAL Posted 3 hours ago Posted 3 hours ago Following on from @mhupp suggestion its pretty easy to do a two item dcl and preset one option. The dcl code is short and can live inside the lisp. (defun wow ( / fo butt val ans) (setq fo (open (setq fname (vl-filename-mktemp "" "" ".dcl")) "w")) (write-line " Presuf : dialog { " fo) (write-line " label =\"Prefix Suffix\" ; " fo) (write-line " : row { " fo) (write-line " : boxed_radio_row { " fo) (write-line " : radio_button { " fo) (write-line " key = \"Rb1\" ; " fo) (write-line " label = \"Prefix\" ; " fo) (write-line " } " fo) (write-line " spacer_1 ; " fo) (write-line " : radio_button { " fo) (write-line " key = \"Rb2\" ; " fo) (write-line " label = \"Suffix\" ; " fo) (write-line " } " fo) (write-line " spacer_1 ; " fo) (write-line " } " fo) (write-line " } " fo) (write-line " spacer_1 ; " fo) (write-line " ok_cancel ; " fo) (write-line " } " fo) (close fo) (setq dcl_id (load_dialog fname)) (if (not (new_dialog "Presuf" dcl_id) ) (exit) ) (setq butt "Rb1") (action_tile "accept" "(setq val (get_tile butt))(done_dialog)") (action_tile "cancel" "(done_dialog)") (start_dialog) (unload_dialog dcl_id) (vl-file-delete fname) (if (= val "1") (setq ans "Pre") (setq ans "Suff") ) (princ ans) ) (wow) 1 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.