Contents

Top  Previous  Next

One can test the contents of a control variable by using several syntax forms below

 

$If %controlvariablename% == texttocompare     statementtoexecute;

$If %controlvariablename%"  == "texttocompare" statementtoexecute;

$If "%controlvariablename%== texttocompare    statementtoexecute;

$If "%system.environmentvarname%== "texttocompare" statementtoexecute;

Notes:

The %controlvariablename% retrieves the text that was placed in the control variable.  The % precedes and succeeds the control variable name.  The quotes need to be used when spaces were included in the text placed therein.
The texttocompare is a text string to compare the text in the control variable with.
The == is a symbol saying compare these two strings.
The % symbol may be redefined using the Escape dollar command.

Examples:

(basicif.gms)

*texttocompare

$Setglobal controlvariablename       comparethistext

$If       %controlvariablename%  == "comparethistexttype1=ok;

$If       %controlvariablename%  ==  comparethistext  type2=ok;

$If      "%controlvariablename%" == "comparethistexttype3=ok;

$If      "%controlvariablename%" ==  comparethistext  type4=ok;

 

*add spaces

$Setglobal controlvariablename       compare this text

$If      "%controlvariablename%" == "compare this texttype1=ok;

$If      "%controlvariablename%" ==  compare this text  type2=ok;

 

generates code that after compilation and looks like the following

 

39  *texttocompare

41  type1=ok;

42  type2=ok;

43  type3=ok;

44  type4=ok;

45

46  *add spaces

47  type1=ok;

 

This code shows that the quoted or uncontrolled quoted control variable name and target text works when spaces are not present.  If spaces are present then the text retrieved by use of %controlvariablename% must be encased in quotes and if the comparison is to work so must be the target text.

 

Often one may wish to see if the text for an item is blank.  This is frequently done with commands like the following

 

$If       "%controlvariablename%a"  == "a"  display "blank it is";

$If not "%controlvariablename%a" == "a" display "it is not blank";

 

which contain the element "%controlvariablename%a" that reduces just to "a" if the named control variable does not have a text entry.

 

Examples employing the environmental variables appear in environvar.gms.