PDA

View Full Version : Using While in Lisp



xspacex
2nd Nov 2009, 09:01 pm
Hello Everyone,

I think I have a simple question, so hopefully I will find my answer…

When I use this code, everything works fine

(while (/= dst1 "400")
(setq dst1 (getstring T "\nEnter Width (400,200): ")))

However, when I try to use “or”, I can’t get it to work

(while (or (/= dst1 "400")(/= dst1 "200"))
(setq dst1 (getstring T "\nEnter Width (400,200): ")))


Thanks in advance!

alanjt
2nd Nov 2009, 09:11 pm
If dst1 doesn't equal 400 or 200 in the or statement, it will return nil. Thus, terminating while. Try and instead of or.

flowerrobot
2nd Nov 2009, 09:19 pm
If your going to use the getstring function, why not go the route of


(initget 7 "200 400")
(setq dst1 (getstring T "\nEnter Width (400,200): "))
But to answer your question try,


(while (not (or (= dst1 "200")(= dst1 "400")))....
Other wise i think you would need an "and" instead of an "or" statement.
Aswell if my memory severs me correct "/=" can cause a probblem now and then, and better useing "not"...Mind you im quiet sinile in my younge age.


Or


(while (not (member dst1 '("200" "400")))....

alanjt
2nd Nov 2009, 09:22 pm
FlowerRobot, initget doesn't work with getstring.

xspacex
2nd Nov 2009, 09:32 pm
Thanks for the responses.
I didn’t have success using (initget 7 "400 200"), because if I enter a value other than 400 & 200, the program will continue. However, using “and” and using (not (or works for the most part, but the problem I am having now, is that if I enter 400 as the width, and then repeat the command a minute later, it will skip the while loop (which is the part that asks to enter the distance) and move to the next step. It seems like it is remembering the distance input the first time the command is used... Any reasons why?
Thanks again

alanjt
2nd Nov 2009, 09:36 pm
Thanks for the responses.
I didn’t have success using (initget 7 "400 200"), because if I enter a value other than 400 & 200, the program will continue. However, using “and” and using (not (or works for the most part, but the problem I am having now, is that if I enter 400 as the width, and then repeat the command a minute later, it will skip the while loop (which is the part that asks to enter the distance) and move to the next step. It seems like it is remembering the distance input the first time the command is used... Any reasons why?
Thanks again
You are not localizing your variables. It's hard to guess w/o seeing the code.

Why not use getreal, getdist or getint to eliminate the user entering alpha characters? You can always convert it to a string if/when you need to.

xspacex
2nd Nov 2009, 09:56 pm
Once this code is done, i will add it to another code we have in the office, which draws radiator lines. This part will help offset the lines...

(defun c:test (/ en1 pt1 and)
(setvar "cmdecho" 0)
(while (not (or (= dst1 "400")(= dst1 "200")))
(setq dst1 (getstring T "\nEnter Width (400,200): "))
)
(setq en1 (entlast))
(setq pt1 (getpoint "\nSpecify Offset Side:"))
(if (= dst1 "400")
(progn
(command "._offset" 400 en1 "_non" pt1 "")
))
(if (= dst1 "200")
(progn
(command "._offset" 200 en1 "_non" pt1 "")
))
(setvar "cmdecho" 1)
(princ)
)

xspacex
2nd Nov 2009, 10:05 pm
You are not localizing your variables.

I had to add dst1 and i had to remove and in my localized variables :)

Thanks everyone

alanjt
2nd Nov 2009, 10:13 pm
Try something like this:


(defun c:test (/ en1 dst1 pt1)
(setvar "cmdecho" 0)
(and (setq en1 (entlast))
(while (not (member dst1 '(200 400)))
(setq dst1 (getreal "\nEnter Width [200/400]: "))
) ;_ while
(setq pt1 (getpoint "\nSpecify Offset Side: "))
(command "_.offset" dst1 en1 "_non" pt1 "")
) ;_ and
(setvar "cmdecho" 1)
(princ)
) ;_ defun

Plus, you can select the 200 or 400 if dynamic mode is on, or a right-click. :)

flowerrobot
3rd Nov 2009, 01:49 am
FlowerRobot, initget doesn't work with getstring.

Whoops was thinking of getkword, but wrote getstring, Was drinking my morning coffeee so you know how it is...

alanjt
3rd Nov 2009, 02:53 am
Whoops was thinking of getkword, but wrote getstring, Was drinking my morning coffeee so you know how it is...
:) For me it would have been a jittery getstrrrriiiiing.