TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Pad on May 06, 2010, 06:54:11 AM

Title: variable value help please
Post by: Pad on May 06, 2010, 06:54:11 AM

Hello

How can I change this bit of lisp so that the previously entered values are shown (if previously run, or a default value of say 4 and 4 if not) and so that these values can be re-used by pressing enter or changed by entering a new value?

      (setq columns (getint "\nEnter number of columns: "))
      (setq rows (getint "\nEnter number of rows: "))

Thanks
Pad
Title: Re: variable value help please
Post by: JohnK on May 06, 2010, 07:14:44 AM
the answer to you're 'remembering' question is 'localization'. the answer to you're 'default answer' question is the empoyment of COND.
since im on my phone, ill leave the code up someone else. :)
Title: Re: variable value help please
Post by: Lee Mac on May 06, 2010, 07:32:34 AM
Perhaps something like this  :-)

Code: [Select]
[color=green];; We need to give it a default value to begin with:[/color]

(or *variable* (setq *variable* 1))

[color=green];; If *variable* has a value, the second expression
;; will not be evaluated, as OR stops evaluating as
;; soon as an expression returns T.[/color]

(setq *variable*
  (cond
    (
      (getint
        (strcat "\nWhats your favourite Number <"
          (itoa *variable*) "> : "
        )
      )
    )
    ( *variable* )
  )
)

[color=green];; If the user enters a number, the first
;; COND expression will return that value
;; (a non-nil value), hence the second will
;; not be evaluated and *variable* will be set
;; to the new value, else the default is used.[/color]

Alternatively, this could be condensed into one statement:

Code: [Select]
(setq *variable*
  (cond
    (
      (getint
        (strcat "\nWhats your favourite Number? <"
          (itoa
            (setq *variable*
              (cond ( *variable* ) ( 1 ))
            )
          )
          " > : "
        )
      )
    )
    ( *variable* )
  )
)
Title: Re: variable value help please
Post by: fixo on May 06, 2010, 07:37:01 AM

Hello

How can I change this bit of lisp so that the previously entered values are shown (if previously run, or a default value of say 4 and 4 if not) and so that these values can be re-used by pressing enter or changed by entering a new value?

      (setq columns (getint "\nEnter number of columns: "))
      (setq rows (getint "\nEnter number of rows: "))

Thanks
Pad

Here is an easy way
Code: [Select]
(if (null columns) (setq columns 10))
(princ (strcat "\nEnter number of columns <" (itoa columns) ">: "))
(setq ncolumns (getint))
(if (= ncolumns nil) (setq ncolumns columns) (setq columns ncolumns))
(if (null rows) (setq rows 10))
(princ (strcat "\nEnter number of rows <" (itoa rows) ">: "))
(setq nrows (getint))
(if (= nrows nil) (setq nrows rows) (setq rows nrows))

~'J'~
Title: Re: variable value help please
Post by: Pad on May 06, 2010, 07:53:32 AM
great stuff, thanks for all the answers and assistance.

Lee, I had this problem with the first code you posted
Command: wg2
Enter number of columns: <4> : 3 bad argument type: fixnump: nil
so have used the second piece of code which works perfectly, thanks.


fixo, thanks a lot.  That code is a lot more easily understood by me and is very helpful.

Regarding localization, would I need to make the program loop?  Currently all I have done is remove the variables from the declaration.

Thanks again
Pad
Title: Re: variable value help please
Post by: Lee Mac on May 06, 2010, 08:03:41 AM
Pad,

There is nothing wrong with the code I posted - how did you use it?
Title: Re: variable value help please
Post by: Pad on May 06, 2010, 08:11:22 AM

It's ok Lee, I take it back.  It was probably a mistake by myself when changing the variable names.  I'm using your second example, the version condensed into one statement which is working perfectly.
Title: Re: variable value help please
Post by: CAB on May 06, 2010, 09:31:28 AM
Code: [Select]
  (or columns (setq columns 1))
  (or rows (setq rows 1))
  (setq columns
         (cond ((getint (strcat "\nEnter number of columns <" (itoa columns) ">: ")))
               (columns)
         )
  )
  (setq rows
         (cond ((getint (strcat "\nEnter number of rows <" (itoa rows) ">: ")))
               (rows)
         )
  )
Title: Re: variable value help please
Post by: CAB on May 06, 2010, 09:42:47 AM
With range check
Code: [Select]
  (or columns (setq columns 1))
  (or rows (setq rows 1))
  (while
    (progn
      (setq tmp
             (cond ((getint (strcat "\nEnter number of columns <" (itoa columns) ">: ")))
                   (columns)))
      (if (< 1 tmp 20) (not (setq columns tmp))
        (princ "\nColumns out of range (1-20), re-enter."))
    )
  )
  (while
    (progn
      (setq tmp
             (cond ((getint (strcat "\nEnter number of rows <" (itoa rows) ">: ")))
                   (rows)))
      (if (< 1 tmp 20) (not (setq rows tmp))
        (princ "\nRows out of range, re-enter(1-20)."))
    )
  )
Title: Re: variable value help please
Post by: Pad on May 06, 2010, 10:13:08 AM
thanks CAB.
so many ways to skin a cat..