TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: hmspe on December 30, 2018, 10:18:54 AM

Title: Get the name of a variable as a string
Post 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.

   
Title: Re: Get the name of a variable as a string
Post by: Grrr1337 on December 30, 2018, 11:20:00 AM
Hi, you're looking for something like this ? :
Code - Auto/Visual Lisp: [Select]
  1. (setq a "hello")
  2. (setq b 123)
  3. (setq c 'world)

Code - Auto/Visual Lisp: [Select]
  1. (defun _printvars ( L )
  2.   (foreach x L
  3.     (print (vl-prin1-to-string (list x ': (eval x))))
  4.   )
  5.   (princ)
  6. )

Code - Auto/Visual Lisp: [Select]
  1.  (_printvars '(a b c))
  2. >>
  3. "(A : \"hello\")"
  4. "(B : 123)"
  5. "(C : WORLD)"
Title: Re: Get the name of a variable as a string
Post by: Lee Mac on December 30, 2018, 11:27:28 AM
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:
Code - Auto/Visual Lisp: [Select]
  1. (defun printit ( var )
  2.     (print var)
  3.     (prin1 (eval var))
  4.     (princ)
  5. )

Code - Auto/Visual Lisp: [Select]
  1. _$ (setq count 123)
  2. 123
  3. _$ (printit 'count)
  4.  
  5. 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:
Code - Auto/Visual Lisp: [Select]
  1. _$ (vl-symbol-name 'count)
  2. "COUNT"
  3. _$ (eval (read "COUNT"))
  4. 123
  5. _$ (vl-symbol-value (read "COUNT"))
  6. 123
Title: Re: Get the name of a variable as a string
Post by: MickD on December 30, 2018, 05:35:53 PM
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.
Title: Re: Get the name of a variable as a string
Post by: hmspe on December 30, 2018, 05:51:11 PM
Thanks for the replies.  The intent is to use this as a de-bugging aid.  Here's what I came up with:
Code: [Select]
(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:
Code: [Select]
(setq a "1234")
(setq b 1234)
(setq c 1234.1234)
(pt 'a)
(pt 'b)
(pt 'c)
(pt (list 'a 'b 'c))
Title: Re: Get the name of a variable as a string
Post by: Lee Mac on December 31, 2018, 05:47:34 AM
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.
Title: Re: Get the name of a variable as a string
Post by: MP on December 31, 2018, 11:47:52 AM
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.
Title: Re: Get the name of a variable as a string
Post by: MickD on January 02, 2019, 04:32:47 PM
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! :)