Jump to content

Define generated variables locally


benhubel

Recommended Posts

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)
)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...