Set
and Setq
In AutoLisp, the process of establishing a value for a symbol is called assignment. The common way to do assignment is to use SETQ. An assignment causes the value of its second argument to become the value of its first argument, which has to be a symbol. A symbol is a lexical variable, declared implicitly.
SET is somewhat like SETQ except that SET does evaluate its first argument. Therefore the first argument must evaluate to the name of a dynamic variable (one whose value is to be created dynamically). The only other difference is that SET takes only two arguments.
Consider the following assignments :
(setq a `var) sets symbol A to VAR. Symbol VAR is not defined.
(set a 2) Evaluates A and sets the result of this evaluation to 2.
(princ var) Returns 2. Symbol VAR is now defined and set to 2.
A common use of the SET assignment is to define symbols at runtime, and is sometimes more efficient than list construction when the number of items is not known in advance. As an illustration of this, the following lisp routine example takes a variable number of points, sorts them by their Y coordinate and draws a line using the sorted points.
(defun c:test ( / j lpt symb)
(setq j 1)
(while (set (read (strcat "point_" (itoa j)))
(getpoint "\nPoint : ")
)
(setq j (1+ j))
)
;; Last point is nil.
(setq j (1 j))
;; Sort the points. See function "sortpoints" below.
(sortpoints j)
;; Initialize the list of points to be passed to (command "_line")
(setq lpt (list ""))
;; Construct the list and set each of our
;; "on the fly" symbols to nil
(while (> j 0)
(setq
symb (read (strcat "point_" (itoa j)))
lpt (cons (eval symb) lpt)
)
(set symb nil)
(setq j (1 j))
)
;; draw the line
(command "_line")
(mapcar 'command lpt)
(princ)
)
;; This function sorts the points by their Y coordinate
(defun sortpoints (j / i cur_y prev_y cur_symb prev_symb point)
(setq i 2)
(while (<= i j)
(setq
cur_symb (read (strcat "point_" (itoa i)))
prev_symb (read (strcat "point_" (itoa (1 i))))
cur_y (cadr (eval cur_symb))
prev_y (cadr (eval prev_symb))
)
(if (> cur_y prev_y)
(progn
(setq point (eval prev_symb))
(set prev_symb (eval cur_symb))
(set cur_symb point)
(if (/= i 2) (setq i (1 i)))
)
(setq i (1+ i))
)
)
)

