Creating incremental variables in AutoLISP

Creating incremental variables in AutoLISP

adaptacad
Advocate Advocate
1,363 Views
5 Replies
Message 1 of 6

Creating incremental variables in AutoLISP

adaptacad
Advocate
Advocate

In the example below I ask the user to enter a value and is given continuity to the code...

(defun c:Test ()
  (setq lista '(1))
  (setq n_forcas (cond ((getint "\nnumber of variables <1>: "))
		       (1)))
  (if (> n_forcas 0)
    (progn
      (cond
	((= n_forcas 1)
	 (progn
	   (setq f1 (getint "\variables 1: "))	   
	   ))
	((= n_forcas 2)
	  (progn
	   (setq f1 (getint "\variables 1: "))
	   (setq f2 (getint "\variables 2: "))
	   ))
	)
      )    (Alert "Invalid Input!")
    )
  (princ)
  )

 

but I would like to create a variable that I could incremental, for example:
 (setq n_forcas (cond ((getint "\ nnumber of variables <1>:"))
(1)))
if I type 1 it will create
(setq f1 (getint "\ variables 1:"))
if I type 2 it will create
(setq f1 (getint "\ variables 1:"))
(setq f2 (getint "\ variables2:"))
if I type 3 it will create
(setq f1 (getint "\ variables 1:"))
(setq f2 (getint "\ variables2:"))
(setq f3 (getint "\ variables3:"))
4, 5, 6 [...]
Is it possible or not?
0 Likes
1,364 Views
5 Replies
Replies (5)
Message 2 of 6

cadffm
Consultant
Consultant
It is possible.

[F1] Read about lisp function SET (not SETQuote)
and READ

Without testing:
(setq n 0)
(repeat numberofvariables
(set (read(strcat "f" (itoa (setq n (+1))))) (getint (strcat "\nVariable"(itoa n)": "))
)

Sebastian

Message 3 of 6

scot-65
Advisor
Advisor
Look into LIST instead?
Only one variable is needed.
A list can be a collection of various items including other lists. (!)
Use CONS or APPEND to build the list.
Use LENGTH to determine how many items are in the list.
Use CAR CADR CADDR and/or NTH to access the items in the list.

Example tested using 3:
(setq a nil n 0 n1 (getint "How many items: "))
(while (< n n1)
(setq a (append a (list (strcat "variable" (itoa (setq n (1+ n)))))))
);while
(princ a)

Command: (while (< n n1) (setq a (append a (list (strcat "variable" (itoa (setq n (1+ n))))))))
("variable1" "variable2" "variable3")

LISP = LISt Programming

???

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

Message 4 of 6

adaptacad
Advocate
Advocate

@scot-65 @cadffm

I don't understand how this would apply to code

0 Likes
Message 5 of 6

john.uhden
Mentor
Mentor

@cadffm is leading you down the correct path.

Since I haven't had a cocktail (yet), I'l just blurt out what comes to mind.

(defun c:Variables ( / N i ans)
  (initget 7)
  (setqq N (getint "\nEnter number of variables do you want to create: "))
  (setq i 1)
  (repeat N
    (and
      (setq ans (getstring T (strcat "\nEnter value for VAR" (itoa i) ": ")))
      (/= ans "")
      (set (read (strcat "VAR" (itoa i))) ans)
    )
    (setq i (1+ i))
  )
  (princ)
)
;; Untested

Now we can also add conditional formatting to convert the string to a REAL or INT, even feet and inches, and maybe even a LIST.

We can also make it where you don't have to choose how many variables to set, as an empty response could stop the cycle.

Bear in mind that these variables will most likely remain global, but there's a way to handle that too.

John F. Uhden

Message 6 of 6

john.uhden
Mentor
Mentor

Hey, dimwit, where is the setqq function defined?  :]

John F. Uhden

0 Likes