Author Topic: variable value help please  (Read 1885 times)

0 Members and 1 Guest are viewing this topic.

Pad

  • Bull Frog
  • Posts: 342
variable value help please
« 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

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: variable value help please
« Reply #1 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. :)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: variable value help please
« Reply #2 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* )
  )
)

fixo

  • Guest
Re: variable value help please
« Reply #3 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'~

Pad

  • Bull Frog
  • Posts: 342
Re: variable value help please
« Reply #4 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

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: variable value help please
« Reply #5 on: May 06, 2010, 08:03:41 AM »
Pad,

There is nothing wrong with the code I posted - how did you use it?

Pad

  • Bull Frog
  • Posts: 342
Re: variable value help please
« Reply #6 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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: variable value help please
« Reply #7 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)
         )
  )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: variable value help please
« Reply #8 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)."))
    )
  )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Pad

  • Bull Frog
  • Posts: 342
Re: variable value help please
« Reply #9 on: May 06, 2010, 10:13:08 AM »
thanks CAB.
so many ways to skin a cat..