User Tools

Site Tools


gams:the_difference_between_set_and_setglobal

The difference between $set and $setglobal

Q: I don' understand why the code below does not yield yes from the display.

   $set cafe no
   $setglobal cafe yes
   display "%cafe%"
----      3 no

The reason is that the the different $set, $setlocal and $setglobal commands do not reference the same variable. They create different variables at different levels of scope. When a variable is referenced (for example by a display statement) is determined by the scoping rules. If there is a local variable by that name that's what you'll get. Otherwise you'll get a viable defined at a higher scope level. You'll see the global variable only if there isn't anything at a lower scope level hiding it.

The example below demonstrates how things work:

   $setlocal cafe local
   display "%cafe%"
   $set cafe scoped
   display "%cafe%"
   $setglobal cafe global
   display "%cafe%"
   $droplocal cafe
   display "%cafe%"
   $drop cafe
   display "%cafe%"
----      2 scoped

----      4 scoped

----      6 scoped

----      8 global

Please also visit chapter Conditional Compilation in the McCarl User Guide for more information.

gams/the_difference_between_set_and_setglobal.txt · Last modified: 2017/09/02 17:16 by support