benhubel Posted November 10, 2017 Posted November 10, 2017 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) ) Quote
David Bethel Posted November 10, 2017 Posted November 10, 2017 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 Quote
benhubel Posted November 10, 2017 Author Posted November 10, 2017 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. Quote
Grrr Posted November 10, 2017 Posted November 10, 2017 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 Quote
Recommended Posts
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.