MastroLube Posted July 7, 2017 Share Posted July 7, 2017 Hello everyone! I've developed 2 programs in visual lisp + opendcl. Unfortunately I've used the same name for few global variables. This cause that if I open the other program, the first doesn't work anymore and I have to load it again. There is a way to fix that or I've to change every variable name in both programs ? Thank you for the hint, Dennis Quote Link to comment Share on other sites More sharing options...
tombu Posted July 7, 2017 Share Posted July 7, 2017 As much as possible avoid using global variables. Many coders avoid using them at all. When needed make them distinctly descriptive, same as function names. While I've used one a few times if you have code with multiple global variables you may want to post it to see what suggestions you may get. Quote Link to comment Share on other sites More sharing options...
BIGAL Posted July 8, 2017 Share Posted July 8, 2017 mastro lube because lisp is a text file you could write a lisp that reads a lisp file and looks for "(setq" then changes it to "(setq ML" and rest of line ie make a copy of the lisp file with the ML added to every variable, also look at start of code for redefine local variable names. ( / x y etc) Quote Link to comment Share on other sites More sharing options...
Grrr Posted July 8, 2017 Share Posted July 8, 2017 As much as possible avoid using global variables. Many coders avoid using them at all. When needed make them distinctly descriptive, same as function names. While I've used one a few times if you have code with multiple global variables you may want to post it to see what suggestions you may get. I second that. (1+) For instance if one has global variable for an integer assigned to the *int* symbol, and the same is used inside MyProgram1 - it would better be named *MyProgram1_int* or something like that, so the symbol name is descriptive enough. Or write it into the registry, again with descriptive name (I saw Lee Mac doing it in his programs). Quote Link to comment Share on other sites More sharing options...
Steven P Posted July 12, 2017 Share Posted July 12, 2017 Would there be anything wrong with keeping the global variables but resetting them at the beginning of the LISP? As an example, I use 'acount' to count things so where I use it, I also add (sstq account 0) Quote Link to comment Share on other sites More sharing options...
ronjonp Posted July 12, 2017 Share Posted July 12, 2017 If you're working with strings you can also use setenv: (setenv "MyGlobal" "1") Quote Link to comment Share on other sites More sharing options...
tombu Posted July 12, 2017 Share Posted July 12, 2017 Would there be anything wrong with keeping the global variables but resetting them at the beginning of the LISP? As an example, I use 'acount' to count things so where I use it, I also add (sstq account 0) Wouldn't setting 'acount' as a local variable do exactly the same thing without all the issues with having it as a global variable? The only reason to use a global variable is to have it's value available the next time it's used in the same drawing in the same session. Quote Link to comment Share on other sites More sharing options...
Steven P Posted July 12, 2017 Share Posted July 12, 2017 Yes Tombu it would - however there have been times when keeping the count going is also useful thing (not just in LISPS, but say in an excel macro and co on), but I used that as a simple example. Without knowing what the original posters global variables are doing, this could be a solution that works for him Quote Link to comment Share on other sites More sharing options...
Grrr Posted July 12, 2017 Share Posted July 12, 2017 If you're working with strings you can also use setenv: (setenv "MyGlobal" "1") Not just strings... if you know what you are doing: ; Example to define custom program, by writing it into the registry: (setenv "MyProgram" (vl-prin1-to-string '(lambda ( / p ) (and (setq p (getpoint "\nSpecify point: ")) (alert (strcat "\nSpecified point: " (vl-prin1-to-string p))) ) ) ) ) "(LAMBDA (/ P) (AND (SETQ P (GETPOINT \"\\nSpecify point: \")) (ALERT (STRCAT \"\\nSpecified point: \" (VL-PRIN1-TO-STRING P)))))" _$ (eval (read (strcat "(" (getenv "MyProgram") ")"))) ; Run the program nil ; Example to store custom variables, by using assoc list (to keep a single registry-name): _$ (setenv "MyVariables" (vl-prin1-to-string '((var1 60)(var2 120)(var3 PI)))) "((VAR1 60) (VAR2 120) (VAR3 PI))" _$ (eval (cadr (assoc 'var3 (read (getenv "MyVariables"))))) 3.14159 Quote Link to comment Share on other sites More sharing options...
ronjonp Posted July 12, 2017 Share Posted July 12, 2017 Agreed .. should have clarified writing strings. I was keeping the example simple as to not confuse the OP. The data can be evaluated as whatever IF you know what you're doing. Nice example BTW. Quote Link to comment Share on other sites More sharing options...
Grrr Posted July 12, 2017 Share Posted July 12, 2017 Nice example BTW. Thanks Ron, I was just clarifying for anyone reading this thread - not trying to tutor you. Quote Link to comment Share on other sites More sharing options...
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.