Jump to content

Few lisp with same global variables: conflict


MastroLube

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

;)

Link to comment
Share on other sites

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.

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