Jump to content
benhubel

Define generated variables locally

Recommended Posts

benhubel

I have been experimenting with generating variables through code rather than defining them manually. The problem is that when the program creates them, they are defined as global variables. Is there a way to generate them as local variables instead?

 

Below is the sample code that I wrote for testing it.

 

;ListToVariables creates variables named testvar0, testvar1, testvar2, etc.
;Each newly created variable contains the contents of the correlated slot from testlist.
(defun c:test ( / testlist )
(setq testlist (list "aaa" "bbb" "ccc" "ddd" "eee"))
(ListToVariables testlist)
)

(defun ListToVariables ( listname / i )
(setq i 0)
(repeat (length listname)
	(set (read (strcat "testvar" (rtos i 2 0))) (nth i listname))
	(setq i (1+ i))
)
(princ)
)

Share this post


Link to post
Share on other sites
David Bethel

You can but it is a RPIA

 

Your best bet is to use a standard name convention for these types that will be used for that session only

 

I use a prefix of gv_ for variable that are global and set for reuse and tv_ for temp variables

 

-David

Share this post


Link to post
Share on other sites
benhubel
You can but it is a RPIA

 

Your best bet is to use a standard name convention for these types that will be used for that session only

 

I use a prefix of gv_ for variable that are global and set for reuse and tv_ for temp variables

 

-David

 

That makes sense, and it sounds like the route I might have to go. Out of curiosity, I'm still interested to see how generated local variables could be done though. If anybody has any examples, or a link to any sort of documentation describing it, I'd love to check it out. The only way I can think of right now is to write it as an intermediate program that writes its own function which declares the variables locally.

Share this post


Link to post
Share on other sites
Grrr

Maybe...

 

; (ListToVariables "hello" '(44 55 88))
(defun ListToVariables ( pref L / varnm i r )
 (setq i 0)
 (foreach x L
   (set (read (setq varnm (strcat pref (itoa (setq i (1+ i)))))) x)
   (setq r (cons varnm r))
 )
 (reverse r)
)

 

(ListToVariables "hello" '(44 55 88)) >> ("hello1" "hello2" "hello3")
hello1 >> 44
hello2 >> 55
hello3 >> 88
hello4 >> nil

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×