Variable name to function

Discussion in 'AutoCAD' started by TCEBob, Apr 2, 2004.

  1. TCEBob

    TCEBob Guest

    I thought this would be easy: Make a function #getnum(numbr strg
    default) to be called from a routine like (setq myvar(#getnum myvar
    "Enter a number" 10)). The function goes ahead and either displays the
    existing global myvar or the default with the string: Enter a
    number:<23>

    No problem, except I can't see a way to convey the variable name to the
    function. You know, so it can check to see whether it exists. If it goes
    as a string then the function line (if varname varname default) breaks
    down.

    rs
     
    TCEBob, Apr 2, 2004
    #1
  2. TCEBob

    Mark Propst Guest

    if i'm understanding your question correctly (maybe not?)
    (setq myvar(#getnum 'myvar "Enter a number" 10))

    is that what you're looking for?
    without seeing the actual function #getnum I can't be sure.
    hth
    Mark
     
    Mark Propst, Apr 2, 2004
    #2
  3. TCEBob

    zeha Guest

    Is this function what you mean
    Don't forget the quote for the variable

    (defun #getnum(var prmpt dflt / mvar)
    (set var (if (setq mvar (getint (strcat "\n" prmpt " <" (itoa dflt)"> "))) mvar dflt))
    )
    (#getnum 'newvar "Enter a number" 10)
    (princ newvar)
     
    zeha, Apr 2, 2004
    #3
  4. TCEBob

    TCEBob Guest

    Should have done the first time. Haven't tried the quote but will when I
    wake up.
    Later I'll expand to include strings.

    rs

    ;DOESN'T WORK YET!!
    ; get global number given string, number, default
    ; Use: (setq size(#Getnum size "\nEnter size:" 12.5))
    (defun #Getnum(varbl strg default / )
    (if (and (numberp default)(not(numberp strg)))
    ;check default is a number and strg is not a number
    (setq varblx (if varbl varbl default) ;if there as a numbr use it,
    else use default
    temp(getreal(strcat strg " <" (rtos varblx) ">: ")) ;offer to user
    ;note that getreal will keep prompting if user enters a string
    ;but will return nil if user presses <enter>.
    varbl(if temp temp varbl) ;if user enters something take it and
    reset the global
    ) ;else use the global
    (princ "\nError: call with global name, string, number.") ;chide the
    programmer
    )
    )
     
    TCEBob, Apr 2, 2004
    #4
  5. TCEBob

    TCEBob Guest

    Almost. I need to check whether mvar already exists and then use it as
    the default.

    Thanks,

    rs
     
    TCEBob, Apr 2, 2004
    #5
  6. TCEBob

    zeha Guest

    (defun #getnum(var prmpt dflt / mvar)
    (if (eval var)
    (set var (if (setq mvar (getint (strcat "\n" prmpt " <" (itoa dflt)"> "))) mvar dflt))
    (set var dflt)
    )
    )
    (#getnum 'myvar "Enter a number" 10)
     
    zeha, Apr 2, 2004
    #6
  7. Is this what you're looking for?

    (defun #getnum (myvar str default / num temp)
    (if myvar
    (setq num myvar)
    (setq num default)
    )
    (setq temp (getint (strcat "\n" str " <" (itoa num)"> ")))
    (if temp
    (setq globalvar temp)
    (setq globalvar default)
    )
    )
    --
    Ken Alexander
    Acad2004
    Windows2000 Prof.

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein
     
    Ken Alexander, Apr 2, 2004
    #7
  8. Zeha,

    Why quote myvar?
    --
    Ken Alexander
    Acad2004
    Windows2000 Prof.

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein
     
    Ken Alexander, Apr 2, 2004
    #8
  9. TCEBob

    TCEBob Guest

    Yes sir. The trick is in setting up a globalvar which is never actually
    referenced but does cause the routine to produce a response. Right?

    Now that I have a working core I will expand it to include strings and
    reals.

    Side question: I've gotten into the habit of phrasing (if (setq (setq
    as
    (setq (if (a (b))) mainly because it works as a condition to while. Is
    there a functional difference?

    (setq num
    (if myvar myvar default)
    )

    Many thanks,

    rs
     
    TCEBob, Apr 2, 2004
    #9
  10. TCEBob

    Tom Smith Guest

    (setq num
    (setq num
    (cond
    (myvar)
    (default)))

    Same thing, one word shorter. I do the same thing, put the test inside the
    setq instead of the reverse.

    In Ken's version, he isn't using the stored globalvar in the user prompt. If
    I understand the purpose, I don't see why you need to pass myvar to the
    function. It should simply use globalvar if it exists, right?

    (defun #getnum (str default / num temp)
    (setq
    num (if globalvar globalvar default)
    temp (getint (strcat "\n" str " <" (itoa num)"> "))
    globalvar (if temp temp num)))

    This returns the new value of globalvar and uses it as the default next
    time.
     
    Tom Smith, Apr 2, 2004
    #10
  11. TCEBob

    TCEBob Guest

    Oh, I think you meant (setq globalvar num) in the last line.

    rs
     
    TCEBob, Apr 2, 2004
    #11
  12. Good point on the global var. Couldn't see the forest cause there
    was a tree stuck in my eye.

    Test before/after setq. To me it's easier to read to test before.
    I guess it's just preference.
    --
    Ken Alexander
    Acad2004
    Windows2000 Prof.

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein
     
    Ken Alexander, Apr 2, 2004
    #12
  13. I think you're right.
    --
    Ken Alexander
    Acad2004
    Windows2000 Prof.

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein
     
    Ken Alexander, Apr 2, 2004
    #13
  14. TCEBob

    Tom Smith Guest

    Test before/after setq. To me it's easier to read to test before.
    As many things are. But you got to admit it's a lot more concise when the
    whole function boils down to a single setq that sets two locals and one
    global.

    Of course it could be streamlined to eliminate at least one of the locals,
    but again it's a readability issue.

    A lot of times I do a number of local defuns, simply to get all their code
    out of the "main" function -- because it's easier for me to read the
    function as a whole that way. Preference again.
     
    Tom Smith, Apr 2, 2004
    #14
  15. TCEBob

    TCEBob Guest

    That could be a problem if I ever want to set a different variable. In
    other words, globalvar is now 31415 and I am now looking to set foofoo
    with a default of 7. The new prompt will stick in 31415.

    Cute trick with cond. Thanks.

    rs
     
    TCEBob, Apr 2, 2004
    #15
  16. I would agree.

    Just for fun. Get rid of both locals.

    I suppose that you could also get rid of setq also.

    (defun #getnum (str dft)
    (setq globalvar
    (cond
    ((getint (strcat "\n" str " <"
    (itoa (if globalvar globalvar dft))"> ")))
    (globalvar)
    (dft)))
    )


    --
    Ken Alexander
    Acad2004
    Windows2000 Prof.

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein
     
    Ken Alexander, Apr 2, 2004
    #16
  17. TCEBob

    TCEBob Guest

    (cond
    ((getint (strcat "\n" str " <"
    (itoa (if globalvar globalvar dft))"> ")))
    (globalvar)
    (dft)))

    Really cute. But we still need to identify which variable we are
    modifying, since globalvar may not apply. Why not just set myvar
    instead?

    (cond
    ((getreal(strcat "\n" str " <
    (rtos (if myvar myvar default)) "> ")))
    (myvar)
    (default)
    )

    rs
     
    TCEBob, Apr 2, 2004
    #17
  18. TCEBob

    TCEBob Guest

    Here it is, as assisted so well. Next, including strings.

    Thanks again,

    rs

    (defun #getvbl (myvar str default / num temp)
    (cond ;neat trick by Tom Smith
    ((getreal(strcat "\n" str " <"
    (rtos (if myvar myvar default)) "> ")))
    (myvar)
    (default)
    )
    )
     
    TCEBob, Apr 2, 2004
    #18
  19. TCEBob

    Tom Smith Guest

    (defun #getvbl (myvar str default / num temp)
    Now you're not setting a global to recall later. As I understood it, that
    was one of your goals.

    If you want this to be really general, there are a lot of cases where the
    user expects his last response to be the default. I think you really want
    one of three possible "default" values: a new value, if you choose to force
    a new one, otherwise the last user response if it's recorded in a global,
    otherwise a fallback value for the global.

    (defun #getvbl (newval str default / num temp)
    (setq num
    (cond
    (newval)
    (globalvar)
    (default)))
    (setq globalvar
    (if (setq temp (getreal (strcat "\n" str " <" (rtos num)) "> "))
    temp
    num)))

    (#getvbl 5 "Number?" nil) to force 5 as the default

    (#getvbl nil "Number?" 5) to use globalvar as the default or fall back to 5
    if globalvar isn't set
     
    Tom Smith, Apr 2, 2004
    #19
  20. TCEBob

    TCEBob Guest

    It does just what I want. When I call it from a routine I have already
    set the variable to global (meaning I did not set it to local). Here it
    is, at work:

    (defun c:dsp( / obj opt) ; Displace then erase original
    (setq odi(#getvbl odi "Displace distance" 5))
    (while
    (setq obj (entsel "\nPick an object to Displace: "))
    (redraw (car obj) 3)
    (setq opt(getpoint "\nSide to Displace: "))
    (command "offset" odi obj opt "")
    (command "erase" obj "")
    )
    (princ))
     
    TCEBob, Apr 2, 2004
    #20
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.