Okay, since I'm going home now I'm just gonna post my write-up (that I was waiting to post till after you did a bit of home work) now. Ill give you the def. of 'cond' here in this post.
Enjoy...
"COND"
Serves as the primary conditional function for AutoLISP
(cond [(test result ...) ...])
The cond function accepts any number of lists as arguments. It evaluates the first item in each list (in the order supplied) until one of these items returns a value other than nil. It then evaluates those expressions that follow the test that succeeded.
Since we know that 'cond' is what we want to use, let's come up with an example to help ya understand the power of a simple 'cond' statment.
(And since I never "GIVE" you the answer, im gonna use an entirely diff example then what you want and make you "work" for your answer. ...lmao!)Lets say that we are building and program that asks the user for their answer to a specific problem.
Im gonna do all the coding on this example so you just sit back and watch.
(All the snipits of code I give you, you should cut and paste into your command line to see how they run.)First off I'm gonna build a seperate procedure to ask the user for their answer.
(This is a nice trick if you have any special formatting or whatever you want to do over and over again.) ;;; GETANS
;;; Prompt the user for an answer. Return 'nil' if no answer is supplied.
;;; Example:
;;; Your answer please: 7
;;; > "7"
(defun getans ( / a)
(setq a (getstring "\nYour answer please: "))
;; ask the user for an anser
(if (eq a "")
;; test to see if the anser is nothing
nil
;; if it is return 'nil' instead of ' "" '
a
;; if its a real answer, return that instead.
);_ end if
);_ end defun
Now that I have a way to ask the user for their answer, I need to know how to use it. We use this procedure like this: (getans) But that dosent help us if we want to capture what the user said and or we want to test that answer for a specific answer so let's use it like this: (setq ans (getans)) Now we are creating a variable and filling it with the users answer. Good now how can I use this to make sure the user actualy answers and dosent hit enter or anything. The answer to that is simeple; use a loop to check to see if the user supplied an answer. *lol*
(while (not ans)
(setq ans (getans))
);_ end while
** Cut and paste this one instead of that one to test it out. **(progn
;;; kinda a 'poor mans defun' for testing pourposes.
(setq ans (getans))
(while (not ans)
(setq ans (getans))
);_ end while
);_ end progn
So now if the user tries to just hit enter and trick us we know. And sice we know that (s)he just hit enter we can keep asking them to answer untill they actualy do.
If the user finaly answers our question we want to test to see if its the answer we were expecting. Since we are expecting the user to answer with either "a" or "b" we need a way to check for both instances.
TIP: And since the user could answer with either a capital letter or a lowercase letter we have at lest four conditions to test for?! *phew!*
(OBTW, There is a couple of easier ways to do this, but I want to keep this real simple for ya so bear with me okay?)(cond
((eq ans "a") (alert "You answered 'a'!"))
((eq ans "A") (alert "You answered 'A'!"))
((eq ans "b") (alert "You answered 'b'!"))
((eq ans "B") (alert "You answered 'B'!"))
(t (setq ans (alert "You did not answer correctly")))
);_ end cond
Did you catch all that? What we just did is:
1. Ask the user for an answer.
2. Check to see if they really did answer and not just his enter
3. Check to see if that answer is either 'a' or 'b'
Lets put all that together into a nice little test package for ya.
;;; GETANS
;;; Prompt the user for an answer. Return 'nil' if no answer is supplied.
;;; Example:
;;; Your answer please: 7
;;; > "7"
(defun getans ( / a)
(setq a (getstring "\nYour answer please: "))
;; ask the user for an anser
(if (eq a "")
;; test to see if the anser is nothing
nil
;; if it is return 'nil' instead of ' "" '
a
;; if its a real answer, return that instead.
);_ end if
);_ end defun
(defun c:MyAnswerTester ( / ans)
;;; defun for testing pourposes.
(setq ans (getans))
;; Prompt the user for an answer
;; if they dont enter an answer ask then again ...untill they do answer.
(while (not ans)
(setq ans (getans))
);_ end while
(cond
((eq ans "a") (alert "You answered 'a'!"))
((eq ans "A") (alert "You answered 'A'!"))
((eq ans "b") (alert "You answered 'b'!"))
((eq ans "B") (alert "You answered 'B'!"))
(t (setq ans (alert "You did not answer correctly")))
);_ end cond
);_ end defun
Were you able to follow all of that? Do you have any questions on any of it?