PDA

View Full Version : Trying to figure out why "While" doesn't stop as advertised in the help documentation


Takuwind
2006-08-04, 12:38 AM
Frustrated - impossible to search forum for "While"

I am trying to figure out why While doesnt stop as advertised in the help documentation.
When I use it, it always goes through an extra time, instead of:

"Evaluates a test expression, and if it is not nil, evaluates other expressions; repeats this process until the test expression evaluates to nil "

For instance, the following:
(defun C:testwhile ()
(setq test 1)
(while (<= test 10)
(print test)
(setq test (1+ test))
)
)Outputs this:

Command: testwhile
1
2
3
4
5
6
7
8
9
10 11
Command:

Any ideas why this is and how to stop this?
Thanks.

[ Moderator Action = ON ] What are [ CODE ] tags... (http://forums.augi.com/misc.php?do=bbcode#code) [ Moderator Action = OFF ]

intergrupocr
2006-08-04, 01:04 AM
If you don't use "(princ)" at the end of your programs any autolisp function will return the last value evaluated. Another important thing is use only "<" instead "<=" because the last loop evaluates "test = 10" = True and then do the next statements, in the last will do (setq test (1+ test)) then test will be 11 and the loop will end. if you use only "<" test will be 10 at the end of the loop.

Takuwind
2006-08-04, 01:26 AM
Thanks... having one of those days...

That also helped me figure out the nil test for while, which I was having problems with too.
Thanks

Takuwind
2006-08-04, 01:52 AM
Ok, so I was wrong, I dont understand the nil test.
This code:

(defun C:UCSCOPY ()
(setq table (tblnext "ucs" t))
(print table)
(prompt "\nFirst")

(while table
(setq table (tblnext "ucs"))
(print table)
(prompt "\nAgain")
)
(prompt "\nEnd")
(princ)
)(prompts are for tracking purposes)
returns this:
Command: ucscopy
((0 . "UCS") (2 . "Test 1") (70 . 0) (10 9.94824 6.66425 0.0) (11 0.901829
0.432093 0.0) (12 -0.432093 0.901829 0.0) (79 . 0) (146 . 0.0))
First
((0 . "UCS") (2 . "Test 2") (70 . 0) (10 30.8981 -5.72374 0.0) (11 0.809488
-0.587137 0.0) (12 0.587137 0.809488 0.0) (79 . 0) (146 . 0.0))
Again
((0 . "UCS") (2 . "Test 3") (70 . 0) (10 54.7626 34.9228 0.0) (11 0.223615
0.974678 0.0) (12 -0.974678 0.223615 0.0) (79 . 0) (146 . 0.0))
Again
((0 . "UCS") (2 . "Test 4") (70 . 0) (10 81.6946 16.0317 0.0) (11 0.95056
0.310541 0.0) (12 -0.310541 0.95056 0.0) (79 . 0) (146 . 0.0))
Again
nil
Again
End
Command:
Why does it go thru the entire While funciton AFTER it becomes nil?

[ Moderator Action = ON ] What are [ CODE ] tags... (http://forums.augi.com/misc.php?do=bbcode#code) [ Moderator Action = OFF ]

Lions60
2006-08-04, 02:36 PM
Here try this not sure if its what your looking for though.



(defun C:UCSCOPY ()
(setq table (tblnext "ucs" t))
(print table)
(prompt "\nFirst")

(while table
(if(setq table (tblnext "ucs"))
(progn
(print table)
(prompt "\nAgain")
);;end of progn
);; end of if
);; end of while
(prompt "\nEnd")
(princ)
)

intergrupocr
2006-08-04, 03:28 PM
Ok, so I was wrong, I dont understand the nil test.
This code:

(defun C:UCSCOPY ()
(setq table (tblnext "ucs" t))
(print table)
(prompt "nFirst")

(while table
(setq table (tblnext "ucs"))
(print table)
(prompt "nAgain")
)
(prompt "nEnd")
(princ)
)(prompts are for tracking purposes)




returns this:Command: ucscopy


((0 . "UCS") (2 . "Test 1") (70 . 0) (10 9.94824 6.66425 0.0) (11 0.901829
0.432093 0.0) (12 -0.432093 0.901829 0.0) (79 . 0) (146 . 0.0))
First
((0 . "UCS") (2 . "Test 2") (70 . 0) (10 30.8981 -5.72374 0.0) (11 0.809488
-0.587137 0.0) (12 0.587137 0.809488 0.0) (79 . 0) (146 . 0.0))
Again
((0 . "UCS") (2 . "Test 3") (70 . 0) (10 54.7626 34.9228 0.0) (11 0.223615
0.974678 0.0) (12 -0.974678 0.223615 0.0) (79 . 0) (146 . 0.0))
Again
((0 . "UCS") (2 . "Test 4") (70 . 0) (10 81.6946 16.0317 0.0) (11 0.95056
0.310541 0.0) (12 -0.310541 0.95056 0.0) (79 . 0) (146 . 0.0))
Again
nil
Again
End
Command:


Why does it go thru the entire While funciton AFTER it becomes nil?



[ Moderator Action = ON ] What are [ CODE ] tags... (http://forums.augi.com/misc.php?do=bbcode#code) [ Moderator Action = OFF ]Thats because you use "(print table)" after "(setq table (tblnext "ucs"))", in the last loop table will be nil and will be printed.

Try the same code with a little modification:

(defun C:UCSCOPY ()
(setq table (tblnext "ucs" t))
;;(print table) remove
(prompt "nFirst")
(while table
(print table) ;;moved
(prompt "nAgain") ;;moved
(setq table (tblnext "ucs"))
)
(prompt "nEnd")
(princ)
)

I hope this help you!!!:)

miff
2006-08-04, 05:28 PM
Another way to skin the cat:

(defun C:UCSCOPY (/ table first)
(while (setq table (tblnext "ucs" (not table)))
(if (not first)
(setq first (princ "\nFirst: "))
(princ "\nAgain: ")
)
(print table)
)
(prompt "\nDone!")
(princ)
)

Takuwind
2006-08-04, 06:46 PM
Damn, thats so elegant, it makes my head spin...


Another way to skin the cat:

(defun C:UCSCOPY (/ table first)
(while (setq table (tblnext "ucs" (not table)))
(if (not first)
(setq first (princ "\nFirst: "))
(princ "\nAgain: ")
)
(print table)
)
(prompt "\nDone!")
(princ)
)

Takuwind
2006-08-05, 12:18 AM
Step by step...
Ok, now why does this not work???
(building off the above list of 32 items)


(defun c:typelist (\ count ucsent)
(setq count 0)
(while (setq ucsent (nth count saveucs))
(print (car ucsent))
(print (type (car ucsent)))
(print (cdr ucsent))
(print (type (cdr ucsent)))
(setq count (+ count 1))
)
)