Results 1 to 4 of 4

Thread: Refer to a variable by string

  1. #1
    Member
    Join Date
    2016-02
    Posts
    25
    Login to Give a bone
    0

    Default Refer to a variable by string

    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

  2. #2
    Certifiable AUGI Addict
    Join Date
    2001-03
    Location
    Tallahassee, FL USA
    Posts
    3,658
    Login to Give a bone
    0

    Default Re: Refer to a variable by string

    Not what getstring is supposed to do, check Functions Reference (AutoLISP). To set var2 as a string to first try (setq first (itoa var2))

  3. #3
    Member
    Join Date
    2008-01
    Posts
    26
    Login to Give a bone
    0

    Default Re: Refer to a variable by string

    It's not the best programming (see my suggestion following it), but the exact answer to your question is:
    Code:
    (setq second (eval(read first)))
    Here's better programming (Use LISt Processing since this is LISP):
    Code:
    (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
    Quote Originally Posted by mattko View Post
    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

  4. #4
    Member
    Join Date
    2016-02
    Posts
    25
    Login to Give a bone
    0

    Default Re: Refer to a variable by string

    Thank you, (vl-doc-ref (read ___)) also works.

Similar Threads

  1. how to replace only a single string by pre decided string.
    By mohammad.raees637333 in forum AutoLISP
    Replies: 3
    Last Post: 2014-04-22, 01:07 PM
  2. Convert string to variable
    By davila.vanegas in forum AutoLISP
    Replies: 8
    Last Post: 2010-07-06, 12:42 PM
  3. Replies: 9
    Last Post: 2006-10-05, 09:42 AM
  4. Replies: 12
    Last Post: 2006-09-18, 09:59 PM
  5. Search block name for string to be set as variable value
    By montana.fox in forum VBA/COM Interop
    Replies: 5
    Last Post: 2006-02-17, 08:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •