View Full Version : Refer to a variable by string
mattko
2017-02-03, 01:21 PM
I'm having trouble with referring to a variable value by string. Searched a few hours, no luck.
Example:
(setq var1 12345)
(setq var2 56789)
(setq var3 10101)
Input a string and store it to a variable:
(setq first (getstring)) lets say "var2"
Set the value of the second variable based on the provided string.
(setq second (somethingsomething first))
>>> 56789
Tom Beauford
2017-02-03, 09:19 PM
Not what getstring (http://help.autodesk.com/view/ACD/2017/ENU/?guid=GUID-B139EFBD-74B7-4276-B422-D2186F7D8D0A) is supposed to do, check Functions Reference (AutoLISP) (http://help.autodesk.com/view/ACD/2017/ENU/?guid=GUID-4CEE5072-8817-4920-8A2D-7060F5E16547). To set var2 as a string to first try (setq first (itoa (http://help.autodesk.com/view/ACD/2017/ENU/?guid=GUID-7E247CA3-95D3-4497-BDE2-6EB0E727DD4D) var2))
tom.haws
2017-02-05, 08:32 PM
It's not the best programming (see my suggestion following it), but the exact answer to your question is:
(setq second (eval(read first)))
Here's better programming (Use LISt Processing since this is LISP):
(setq vars (list (list "1" 12345)(list "2" 56789)(list "3" 10101)))
(initget 1 "1 2 3")(setq first (getkword "\nSpecify a variable [1/2/3]: "))
(setq second (cadr (assoc first vars)))
Let me know if that doesn't make sense or does not do what you need.
Tom
I'm having trouble with referring to a variable value by string. Searched a few hours, no luck.
Example:
(setq var1 12345)
(setq var2 56789)
(setq var3 10101)
Input a string and store it to a variable:
(setq first (getstring)) lets say "var2"
Set the value of the second variable based on the provided string.
(setq second (somethingsomething first))
>>> 56789
mattko
2017-02-06, 12:00 PM
Thank you, (vl-doc-ref (read ___)) also works.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.