TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: hmspe on December 30, 2018, 10:18:54 AM
-
Is there a way to convert a variable name to a string? I'd like to create a function that could be passed a variable that would print the variable name then the value.
Example: if COUNT is a variable with the value 1234 then (printit count) would return "count: 1234"
This is easy to do if I pass in the variable name as a string [ie., (printit "count")] but I'm not finding a way to get a string representing the name of the variable. Probably trivial to do but it is not something I've had to do before and I'm not finding the right combination of search terms to find anything useful.
Thanks in advance.
-
Hi, you're looking for something like this ? :
(_printvars '(a b c))
>>
"(A : \"hello\")"
"(B : 123)"
"(C : WORLD)"
-
You would need to pass the variable to the "printit" function as an unevaluated symbol (using either the apostrophe or the quote function (http://lee-mac.com/quote.html)), since the symbol will otherwise be evaluated to yield its value, with the value then passed to the function (at which point the function has no knowledge of the original symbol to which the value had been assigned).
Here is an example:
123
_$ (printit 'count)
COUNT 123
If you really wanted to pass the variable as a string, then you can use the vl-symbol-name function to convert the symbol to a string, and then use (eval (read)) or (vl-symbol-value (read)) to evaluate the supplied string:
-
I'm weak at Lisp so this is for my own curiosity, could you create a symbol table/map that `printit` looks at for the symbol and returns the stored value?
Of course, you wouldn't want to be storing all your var's in a symbol table but it would be helpful for global config's and variables perhaps.
-
Thanks for the replies. The intent is to use this as a de-bugging aid. Here's what I came up with:
(defun pt ( x )
(if (listp x)
(foreach memb x
(prin (strcat (vl-prin1-to-string memb) ": " (vl-prin1-to-string (eval memb))))
(print)
)
(prin (strcat (vl-prin1-to-string x) ": " (vl-prin1-to-string (eval x)) "\n"))
)
(princ)
)
Tested with:
(setq a "1234")
(setq b 1234)
(setq c 1234.1234)
(pt 'a)
(pt 'b)
(pt 'c)
(pt (list 'a 'b 'c))
-
I'm weak at Lisp so this is for my own curiosity, could you create a symbol table/map that `printit` looks at for the symbol and returns the stored value?
Of course, you wouldn't want to be storing all your var's in a symbol table but it would be helpful for global config's and variables perhaps.
You could use the atoms-family function to that effect, but this would of course be less efficient than evaluating the symbol directly.
-
Thanks for the replies. The intent is to use this as a de-bugging aid.
Long in the tooth (almost 20 years old) but you may find this interesting:
(http://www.theswamp.org/screens/mp/atoms.png)
See the variables ... (http://www.theswamp.org/index.php?topic=4201.msg50200#msg50200)
Cheers.
-
I'm weak at Lisp so this is for my own curiosity, could you create a symbol table/map that `printit` looks at for the symbol and returns the stored value?
Of course, you wouldn't want to be storing all your var's in a symbol table but it would be helpful for global config's and variables perhaps.
You could use the atoms-family function to that effect, but this would of course be less efficient than evaluating the symbol directly.
hmm, mysterious and cooky :P
thanks Lee! :)