4.6 Using &Variables

"$Enclose &Variables in Double Quotes

We have now got a value into &1. Let us see what happens when we try to use it.

The safest way of setting one amper variable to another is to enclose the source variable in double quotes:

AMPSET &2 = "&1"

This works regardless of whether &1 contains leading, embedded or trailing blanks, or quotes. It does not change the case of &1. If you omit the double quotes, strings which begin and end with quotes will have them stripped off. If you put the &1 in single quotes and &1 happens to start with a single quote, the result will be to set &2 equal to the null string.

Whenever a command references any amper variables, it is safest to encase the amper variable in double quotes. If you do not, and the amper variable consists of more than one token, this may result in an unexpected number of tokens and cause unexpected results or an error.

For instance suppose that &1 contains “   Hello world   ”, i.e. a string consisting of three blanks followed by the words Hello world and then three more blanks. The command:

APPEND MYSET &1 "This is a message"

is interpreted as

APPEND MYSET    Hello world    "This is a message"

The first token after MYSET is “   Hello”. As APPEND strips blanks off tokens which are not encased in quotes, “Hello” is added as a new Code to MYSET. The Description is set to “world    "This is a message” .

If we encase &1 in double quotes

APPEND MYSET "&1" "This is a message"

the result is to add “   Hello world   ” as a Code to MYSET with “"This is a message” in the Description.

Using &Variables in Tests

You also need to encase amper variables in double quotes in IF tests in macros. If &1 is “   Hello world   ” the statement:


is interpreted as

IF Hello world = "Y" THEN GOTO LABEL1

and so fails with the error “Invalid IF statement” because “world” is not a logical operator.

The test works properly if it is written with the amper variable encased in double quotes:


To test whether an amper variable is null, concatenate it with something and test it against the other thing, e.g.

IF ".&1" = "." THEN GOTO LABEL1

Because the values of amper variables are substituted whenever they appear, it is hard to put the name of an amper variable into an amper variable. To do this, set another amper variable to the character & or the character $ and then concatenate them as required. Thus to set the value of $1 to the string “$1” write:

AMPSET &0 = "$"
AMPSET $1 = "&01"

Save and Restore &Variables in Rules

When writing code which uses amper variables in rules, e.g. to get properties of sets or tables using GETCAT, it is best to save the current values of the amper variables in rule variables and then restore them afterwards. This ensures that if some other code is using that amper variable, it will continue to work:

IF      ?DOLLAR1 = $1
AND     ?DESC = $1
AND     $1 = ?DOLLAR1 

Back                                Next