Author Topic: Get the name of a variable as a string  (Read 398 times)

0 Members and 1 Guest are viewing this topic.

hmspe

  • Bull Frog
  • Posts: 334
Get the name of a variable as a string
« 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.

   
"Science is the belief in the ignorance of experts." - Richard Feynman

Grrr1337

  • Swamp Rat
  • Posts: 675
Re: Get the name of a variable as a string
« Reply #1 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)"
(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)
  )
)

Lee Mac

  • Seagull
  • Posts: 12170
  • London, England
Re: Get the name of a variable as a string
« Reply #2 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), 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
« Last Edit: December 30, 2018, 11:33:11 AM by Lee Mac »

MickD

  • Gator
  • Posts: 3232
  • !false...it's funny 'cause it's true!
Re: Get the name of a variable as a string
« Reply #3 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.
A programmer's wife tells him, "Run to the store and pick up a loaf of bread. If they have eggs, get a dozen."

The programmer comes home with 12 loaves of bread.

hmspe

  • Bull Frog
  • Posts: 334
Re: Get the name of a variable as a string
« Reply #4 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))
"Science is the belief in the ignorance of experts." - Richard Feynman

Lee Mac

  • Seagull
  • Posts: 12170
  • London, England
Re: Get the name of a variable as a string
« Reply #5 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.

MP

  • Seagull
  • Posts: 17300
Re: Get the name of a variable as a string
« Reply #6 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:



See the variables ...

Cheers.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About

MickD

  • Gator
  • Posts: 3232
  • !false...it's funny 'cause it's true!
Re: Get the name of a variable as a string
« Reply #7 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! :)
A programmer's wife tells him, "Run to the store and pick up a loaf of bread. If they have eggs, get a dozen."

The programmer comes home with 12 loaves of bread.