TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Ben Clark on December 13, 2018, 03:59:26 PM

Title: Variable containing a variable name
Post by: Ben Clark on December 13, 2018, 03:59:26 PM
I'm not a programmer by trade, so my question probably has a simple answer.

Is there a way for a variable to contain a variable name in autolisp?

For instance

Code: [Select]
(repeat 10
     (setq
        foo (nth i somelist)
        foo somequantity
        i (+ i 1)
      )
)

except that the items in "somelist" are strings that represent variable names I'd like to assign to foo during loop iterations, and the second time foo appears I'd like it to evaluate to the variable name.

Is this possible??   :thinking:
Title: Re: Variable containing a variable name
Post by: Lee Mac on December 13, 2018, 04:11:29 PM
Processing lists of variables and creating multiple variables on-the-fly are usually a red flag that you should be using lists instead to manipulate your data; but if you really wish to proceed down this route, then consider the read & eval functions:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq myvar 123)
  2. 123
  3. _$ (setq foo "myvar")
  4. "myvar"
  5. _$ (read foo)
  6. MYVAR
  7. _$ (eval (read foo))
  8. 123

Note that you needn't only use strings for this purpose, unevaluated symbols can be used in the same way:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq myvar 123)
  2. 123
  3. _$ (setq foo 'myvar)
  4. MYVAR
  5. _$ (eval foo)
  6. 123
Title: Re: Variable containing a variable name
Post by: VovKa on December 13, 2018, 05:08:53 PM
Is this possible??   :thinking:
Code: [Select]
(setq foo (nth i somelist)
      i   (+ i 1)
)
(set (read foo) somequantity)
Title: Re: Variable containing a variable name
Post by: Grrr1337 on December 13, 2018, 05:40:11 PM
As a possible extension of this topic, this thread (http://www.theswamp.org/index.php?topic=54171.0) might be of interest. :)
Title: Re: Variable containing a variable name
Post by: JohnK on December 14, 2018, 12:23:43 PM
Along with information already given; the method I think you are starting to describe is along the lines of a LET statement--a fun little structure to play with, if you ask me-.

Code - Auto/Visual Lisp: [Select]
  1. (defun let (bindings body)
  2.       ;; let for autolisp.
  3.       ;;
  4.       ;; Ex:
  5.       ;; (let '((x 10) (y 20))                         ; bindings
  6.       ;;      '((princ "\nLocal values of X and Y: ")  ; body
  7.       ;;        (prin1 x)
  8.       ;;        (prin1 y)
  9.       ;;        (princ)
  10.       ;;     )
  11.       ;;   )
  12.       ;;
  13.    (eval
  14.      (cons (append (list 'lambda (mapcar 'car bindings)) body)
  15.            (mapcar 'cadr bindings))) )

An example for you to try in AutoCAD.

Code - Auto/Visual Lisp: [Select]
  1. ( (lambda ( / let)
  2.  
  3.     (defun let (bindings body)
  4.       ;; let for autolisp.
  5.       ;;
  6.       ;; Ex:
  7.       ;; (let '((x 10) (y 20))                         ; bindings
  8.       ;;      '((princ "\nLocal values of X and Y: ")  ; body
  9.       ;;        (prin1 x)
  10.       ;;        (prin1 y)
  11.       ;;        (princ)
  12.       ;;     )
  13.       ;;   )
  14.       ;;
  15.       (eval
  16.         (cons (append (list 'lambda (mapcar 'car bindings)) body)
  17.               (mapcar 'cadr bindings))) )
  18.  
  19.     (initget 6)
  20.     (let
  21.       ;; bindings
  22.       '(
  23.         (rad
  24.           (cond
  25.             ((getdist "\nCircle Radius [1]: "))
  26.             (1))
  27.           )
  28.         (seg
  29.           (cond
  30.             ((getint "\nCircle Segmemts [16]: "))
  31.             (16))
  32.           )
  33.         )
  34.  
  35.       ;; body
  36.       '(
  37.         (setq ang 0
  38.               inc (/ (* pi 2) seg)
  39.               p 0)
  40.  
  41.         (repeat seg
  42.                 (set (read (strcat "P" (itoa p)))
  43.                      (polar '(0 0 0) ang rad))
  44.  
  45.                 (setq ang (+ ang inc)
  46.                       p (1+ p))
  47.                 (print (read (strcat "P" (itoa p))))
  48.                 )
  49.  
  50.         )
  51.       )
  52.     )
  53.  )
Title: Re: Variable containing a variable name
Post by: Ben Clark on December 14, 2018, 06:23:50 PM
Thanks all for your help! My question has been thoroughly answered.