Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Remember string

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
k005
452 Views, 6 Replies

Remember string

Hi sll,

 

I want it to remember the default character and enter it. But I think I'm making a mistake...

 

I am not getting the desired result.

 

Thanks in advance for the help friend.

 

(if (= ygY nil)(setq ygY "-"))
(setq  ygY_t (getstring (strcat "\nReferans karakteri giriniz <"ygY"> :")))
(if (/= ygY_t "")(setq ygY ygY_t))
(setq ygY ygY_t)

 

6 REPLIES 6
Message 2 of 7
ВeekeeCZ
in reply to: k005

I would use this. *ygY* marks a globe variable

 

  (if (= *ygY* nil) (setq *ygY* "-"))
  (setq  ygY (getstring (strcat "\nReferans karakteri giriniz <"*ygY*"> :")))
  (if (= ygY "")      ; if Enter
    (setq ygY *ygY*)  ; working var to pre-defined
    (setq *ygY* ygY)) ; saved value for the future usage.

 

Message 3 of 7
k005
in reply to: ВeekeeCZ

Thank you so much. Ok. 🤗

Message 4 of 7
john.kaulB9QW2
in reply to: k005

Don't mind me but I see this construct used a lot and most of the time it looks to be the same code just copied and pasted over and over which tends to make code blocks/functions messy (-i.e. by not understanding which portions need to be copied or not changing comments or any number of reasons).

 

This method if used multiple times can be useful enough to build a library function from. I'll call the library function `getans'.

(defun getans ( iffalse / answer)
    ;; getans
    ;;
    ;; Prompt the user for an answer. Return 'nil' if no answer is supplied.
    ;;
    ;; EX: 
    ;;   Your answer please: 7
    ;;   > "7"
  (setq answer (getstring (strcat "\nYour answer please <" iffalse ">: ")))
    ;; ask the user for an anser
  (if (eq answer "")
    ;; test to see if the anser is blank string
    
    iffalse
    ;; if it is return 'iffalse' instead of ' "" '
    
    answer
    ;; if its a real answer, return that instead.
    
    );_ end if
  );_ end defun

Now we can use it like:

;;; ...
(setq *ygY* (cond (*ygY*) ("-")))       ;; -Set global variable to a default if not already set.
                                        ;;  if variable is set already, do not override that value.
(getans *ygY*)                          ;; -Prompt for answer from user and use a default if enduser 
                                        ;;  answers with enter -i.e. empty string.
;;; ...

The global variable portion can also be used to construct a generic function if it is a construct that is often repeated as well. I'll call it `set-if'.

(defun set-if ( var iffalse )
    ;; set-if
    ;; sets a variable to a value if the vaiable has NOT been set previously.
    ;; EX:
    ;;     (set-if 'a 1)
    (set var (cond ((eval var)) (iffalse)))
  )


Now our code can be clean(er)--and we can focus on our comments (to make or life easier later)-.

;;; ...
(set-if '*ygY* "-")                     ;; -Set global variable to default ("-") if not already set.
(getans *ygY*)                          ;; -Prompt for answer from end user and use default stored 
                                        ;;  in global variable if end user answers with enter.
;;; ...

 

another swamper
Message 5 of 7
k005
in reply to: john.kaulB9QW2

@ВeekeeCZ 

 

Our friend has solved the problem...

 

* and these are the solutions I was looking for. not fishing. 😉

Message 6 of 7
k005
in reply to: john.kaulB9QW2

Thank you

Message 7 of 7
Sea-Haven
in reply to: k005

Another way.

 

(if (not AH:getvalsm)(load "Multi Getvals.lsp"))
(if (= *ygY* nil) (setq *ygY* "-"))
(setq *ygY* (car (AH:getvalsm (list "Enter values " "Input new val or OK " 5 4 *ygY*))))

SeaHaven_0-1672200404636.png

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

State of Sustainability Webinar


AutoCAD Inside the Factory