Turn setq into string?

Discussion in 'AutoCAD' started by Sage Cowsert, Feb 2, 2004.

  1. Sage Cowsert

    Sage Cowsert Guest

    I posted this in a different thread. It really should be in its own. Ok how
    now how do I do this?

    (setq Bob "is cool")

    to read > "Bob is cool"

    ?? (strcat (read bob) bob) ??
     
    Sage Cowsert, Feb 2, 2004
    #1
  2. Sage Cowsert

    Doug Broad Guest

    Sage,
    Why? What's wrong with

    (strcat "Bob" bob)
     
    Doug Broad, Feb 2, 2004
    #2
  3. Sage Cowsert

    Devin Guest

    (setq Bob "is cool")
    Try...

    (strcat (vl-symbol-name 'bob) " is cool")

    HTH,

    Devin
     
    Devin, Feb 2, 2004
    #3
  4. Sage Cowsert

    Devin Guest

    Or perhaps this is more approprate for what I think you might be doing...

    (read (strcat (vl-symbol-name (quote bob)) " is cool"))

    The above returns the symbol name 'bob is cool'. Except you can't have
    spaces in symbol names.

    If you have a symbol name assigned to the variable 'bob' then leave out the
    quote...

    (setq bob "I am")
    (read (strcat (vl-symbol-name bob) " cool"))

    returns the symbol name 'I am cool'. Again, no spaces in symbol names.

    Devin
     
    Devin, Feb 2, 2004
    #4
  5. Sage Cowsert

    Doug Broad Guest

    Hi Devin.
    Just curious. What you have posted
    is interesting but how would either be
    better than what I posted or have a
    more general application. Perhaps
    if he wanted a nested solution where
    (setq name "bob")
    (setq bob "is cool")
    (strcat name " " (eval (read name)))

    would make more sense.


    Being able to do a thing is different than
    knowing whether a thing should be done
    or is helpful to do. ;-)

    Wouldn't lists, especially association lists,
    be more appropriate than such code?

    Regards,
    Doug
     
    Doug Broad, Feb 2, 2004
    #5
  6. Sage Cowsert

    Sage Cowsert Guest

    Devin's code is what I'm looking for. I'm using this code to store info into
    a dictionary. So I'd like the setq's name stored with the setq'd value. This
    is what I came up with.

    (defun DT_STANDARDS_DICTPUT (VALUE /)
    (vlax-ldata-put "STANDARDS" (vl-symbol-name (quote VALUE)) VALUE)
    )

    Given that

    (setq CPCS_TEXTSIZE 0.25)

    (DT_STANDARDS_DICTPUT CPCS_TEXTSIZE)

    I can now store "CPCS_TEXTSIZE" with the value of 0.25. Before I was
    entering this as

    (DT_STANDARDS_DICTPUT "CPCS_TEXTSIZE" CPCS_TEXTSIZE)

    Which you can see was just entering the same thing twice, extra work. I'm
    just looking for a better mouse trap. :)

    Thanks guys for the help.
     
    Sage Cowsert, Feb 2, 2004
    #6
  7. Sage Cowsert

    Doug Broad Guest

    Pardon me but (vl-symbol-name (quote value)) will
    always return "value". Look at my last post for a
    better way.
     
    Doug Broad, Feb 2, 2004
    #7
  8. Sage Cowsert

    Devin Guest

    Sage,

    You'll need to use quote to pass the variable name thru...

    instead of > (DT_STANDARDS_DICTPUT CPCS_TEXTSIZE)

    use (DT_STANDARDS_DICTPUT (quote CPCS_TEXTSIZE))

    Devin
     
    Devin, Feb 2, 2004
    #8
  9. Sage Cowsert

    Doug Broad Guest

    Here are two ways to do something similar to
    what you want depending on your input preferences:

    (defun test (sym)
    (list
    sym
    (eval (read sym))))

    (defun test2 (qsym)
    (list
    (vl-symbol-name qsym)
    (eval qsym)))

    (test "bob")
    or
    (test2 'bob)
     
    Doug Broad, Feb 2, 2004
    #9
  10. Sage Cowsert

    Sage Cowsert Guest

    I'm glad you guys are around. The value thing got by me. :) Ok here's what
    i've come up with.

    (defun DT_STANDARDS_DICTPUT (VALUE /)
    (vlax-ldata-put "STANDARDS" (vl-symbol-name VALUE) (eval VALUE))
    )

    Given that

    (setq CPCS_TEXTSIZE 0.25)

    (DT_STANDARDS_DICTPUT 'CPCS_TEXTSIZE)
     
    Sage Cowsert, Feb 2, 2004
    #10
  11. Sage Cowsert

    Doug Broad Guest

    Glad to help.
     
    Doug Broad, Feb 2, 2004
    #11
  12. Sage Cowsert

    Devin Guest

    Hi Doug,

    My actual code is a little off, I should have written...

    (setq bob (quote Iam))
    (read (strcat (vl-symbol-name bob) " cool"))

    Although he's not using the way I thought he might be, there is a use for
    working with symbol names instead of strings, perhaps for efficiency... For
    example, if you want to access a variable's value and set a value to it
    later...

    (setq var 0.125)
    (defun func (symbol / )
    (princ "\npre value:")(princ (vl-symbol-value symbol))
    (set symbol 0.25)
    (princ "\npost value:")(princ (vl-symbol-value symbol))
    )

    (func 'var)
    returns...
    "pre value: 0.125"
    "post value: 0.25"

    Of course, like I think you were suggesting, you could just use a string and
    read it into a symbol name as well. But it makes things a little easier and
    cleaner to pass the variable itself with a qoute then converted into a
    string.

    But I know there are useful uses for both ways. For instance, if you have a
    list of tile names in a dialog box and you want to assign a default to each
    one based on what the user entered last, you could use the tile string names
    as the variable names for their own default values. And then later populate
    the dialog with the defaults stored in the variable names. So I don't think
    it matters so much, just what the current application is.

    Devin
     
    Devin, Feb 2, 2004
    #12
  13. Sage Cowsert

    Devin Guest

    Then you don't need to use quote in the function itself, just to pass the
    variable thru.
    ie:

    (defun DT_STANDARDS_DICTPUT (VALUE /)
    (vlax-ldata-put "STANDARDS" (vl-symbol-name VALUE) (vl-symbol-value
    VALUE))
    )

    Given that

    (setq CPCS_TEXTSIZE 0.25)

    (DT_STANDARDS_DICTPUT 'CPCS_TEXTSIZE):<< notice the quote
     
    Devin, Feb 2, 2004
    #13
  14. Sage Cowsert

    Devin Guest

    Doug,

    (vl-symbol-value)
    might be a better approach then using eval. It's kinda like hitting thumb
    tack with a sledge hammer ;)

    Devin
     
    Devin, Feb 2, 2004
    #14
  15. Sage Cowsert

    Doug Broad Guest

    Good point. Pick my excuse ;-)
    (defun excuses (which)
    (cond
    (
    (cdr (assoc which
    '((1 ."Eval is less typing.")
    (2 . "I've always done it that way.")
    (3 . "Ya can't teach an ole Dawg new tricks.)))))
    (t "Eh. What'yda say?"))
     
    Doug Broad, Feb 2, 2004
    #15
  16. Sage Cowsert

    Devin Guest

    Thank goodness for that cond function :)~
     
    Devin, Feb 2, 2004
    #16
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.