Author Topic: Variable containing a variable name  (Read 2313 times)

0 Members and 1 Guest are viewing this topic.

Ben Clark

  • Newt
  • Posts: 94
Variable containing a variable name
« 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:

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Variable containing a variable name
« Reply #1 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

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: Variable containing a variable name
« Reply #2 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)

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Variable containing a variable name
« Reply #3 on: December 13, 2018, 05:40:11 PM »
As a possible extension of this topic, this thread might be of interest. :)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Variable containing a variable name
« Reply #4 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.  )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Ben Clark

  • Newt
  • Posts: 94
Re: Variable containing a variable name
« Reply #5 on: December 14, 2018, 06:23:50 PM »
Thanks all for your help! My question has been thoroughly answered.