Jump to content

AutoLisp Loop funtion to redefine variable


benmc

Recommended Posts

I have what will probably be a basic question for a lot of you. It revolves around LISP programming, IF statements and looping. See the code below just as a simple example.

 

Basically what I am trying to get is..using the IF statement against two real number variables.. if "A" is larger than "B", I want the user to be alerted to this fact, and then redirected to the top of the program to input the variables again. And if "A" is not larger, then do nothing. Any help would be greatly appreciated. Thanks

 

(defun c:testif ()
(setq a (getreal "\nEnter a Number : ")
      b (getreal "\nEnter Second Number : ")
);setq
(if (> a b)
	(prompt "\nFirst number is larger")
	
);if
  (princ)
);defun
(princ)

Link to comment
Share on other sites

(defun c:testif	(/ a b)
 (while
   (progn
     (setq a (getreal "\nEnter a Number : ")
    b (getreal "\nEnter Second Number : ")
     )					;setq
     (cond ((or (not a) (not b))
     (prompt "\nPlease specify the values")
     T
    )
    ((> a b) (prompt "\nFirst number is larger") T)
    (T nil)
     );cond
   );progn
 );while
 (princ)
)

Link to comment
Share on other sites

Make the input a seperate defun within the body of your master code the other way is to swap a & b

 

(defun testif () 
(alert "Enter Val1 less than val 2")
   (setq a (getreal "\nEnter a Number : ") 
         b (getreal "\nEnter Second Number : ") 
   );setq 
   (if (> a b) 
        (progn
        (Alert "First number is larger\n please try again") 
        (testif)
        )
   );if 
  (princ) 
);defun 
(princ) 

master code .....
(testif) ; returns a < b

Link to comment
Share on other sites

Make the input a seperate defun within the body of your master code the other way is to swap a & b

 

(defun testif () 
(alert "Enter Val1 less than val 2")
   (setq a (getreal "\nEnter a Number : ") 
         b (getreal "\nEnter Second Number : ") 
   );setq 
   (if (> a b) 
        (progn
        (Alert "First number is larger\n please try again") 
        (testif)
        )
   );if 
  (princ) 
);defun 
(princ) 

master code .....
(testif) ; returns a < b

 

Thanks bigal! What if I were to have more than just the two variables. For instance, maybe i had 6 variables (a,b,c,d,e,f) and wanted to compare "a against b", then "c against d", and "e against f". Would I simply write the same code you have suggested above, 3 times consecutively? Just replacing the variable names.

Link to comment
Share on other sites

Assuming I have understood what you are looking to achieve, here is another method, allowing for any number of values:

(defun c:test ( / l x )
   (if (setq x (getreal "\nEnter a number: "))
       (progn
           (setq l (list x))
           (while (setq x (getreal (strcat "\nEnter a number greater than " (rtos (car l)) " <done>: ")))
               (if (<= x (car l))
                   (princ (strcat "\nNumber must be greater than " (rtos (car l))))
                   (setq l (cons x l))
               )
           )
       )
   )
   (reverse l)
)

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...