Author Topic: variable name  (Read 1999 times)

0 Members and 1 Guest are viewing this topic.

jsr

  • Guest
variable name
« on: August 30, 2012, 06:00:32 AM »
Hi all

Is it possible to use value of a variable as the name of another variable. For example (setq a "st") and then use st as the name of some other variable. Note that the value "st" is determined during program execution for example it may represent name of a block.

Thanks.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: variable name
« Reply #1 on: August 30, 2012, 06:16:46 AM »


perhaps try

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (setq a "st"
  3.       b "mo"
  4. )
  5.  
  6.  
  7. ;;< .. >
  8.  
  9. (set 'a 42)
  10.  
  11. (set (quote b) "apples")
  12.  
  13.  
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: variable name
« Reply #2 on: August 30, 2012, 06:24:57 AM »
Definately! Also try looking at the read & eval functions. E.g.
Code - Auto/Visual Lisp: [Select]
  1. (setq st "Some arb data here")
  2. (setq a "st")
  3. (read a) ; Returns a symbol st
  4. (print (eval (read a))) ;Evaluates the symbol st which is read from the contents of a and thus returns "Some arb data here"
  5. (print st) ;Returns the same thing "Some arb data here"
  6. (set (read a) "Changed data")
  7. (print (eval (read a))) ;Now it returns "Changed data"
  8. (print st) ;Same again "Changed data"
So see the read as something which converts a string into a lisp expression.

And eval as the opposite of quote.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: variable name
« Reply #3 on: August 30, 2012, 07:43:06 AM »
perhaps try

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (setq a "st"
  3.       b "mo"
  4. )
  5.  
  6.  
  7. ;;< .. >
  8.  
  9. (set 'a 42)
  10.  
  11. (set (quote b) "apples")
  12.  
  13.  

The above is no different to:

Code - Auto/Visual Lisp: [Select]
  1. (setq a "st"
  2.       b "mo"
  3. )
  4.  
  5. ;;< .. >
  6.  
  7. (setq a 42
  8.       b "apples"
  9. )

Since 'setq' is simply a convenience function, short for (set (quote <symbol>) <value>)

I believe the OP was looking to bound a value to the symbol equivalent of the string "st", e.g.:

Code - Auto/Visual Lisp: [Select]
  1. _$ (setq a "st")
  2. "st"
  3. _$ (setq b "mo")
  4. "mo"
  5. _$ (set (read a) 42)
  6. 42
  7. _$ (set (read b) "apples")
  8. "apples"
  9.  
  10. ;; Evaluate variables:
  11. _$ a
  12. "st"
  13. _$ b
  14. "mo"
  15. _$ st
  16. 42
  17. _$ mo
  18. "apples"

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: variable name
« Reply #4 on: August 30, 2012, 07:46:48 AM »
feature set and read very slowly ...

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: variable name
« Reply #5 on: August 30, 2012, 09:04:56 AM »


ahhh ! I misread the question :(
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.