4.5 Setting up &Variables

Character Data Type

Amper variables are always character. If the value appears to be a number it is actually a character string comprising a series of digits. They are set using statements which are handled by the command processor. This operates under its own rules which interact with those for amper variables. We shall consider the command processor first.

Tokenising and Blanks

The command processor processes statements character by character from left to right. For some purposes the command is regarded as being composed of tokens delimited by white space (i.e. one or more blanks). If the command processor is using tokens and it comes across a double quote it considers everthing from there up to and including a matching double quote to be a single token. If it does not find a matching double quote it continues to use white space as the token delimiter.

The AMPSET command is processed character by character but ignores trailing blanks. Thus the statement

AMPSET &1 =    Hello world   

(with four blanks between “=” and “Hello” and three after “world”) is the same as

AMPSET &1 =    Hello world

(with four blanks between “=” and “Hello” but none after “world”) but different from

AMPSET &1 = Hello  world

(with two blanks between “Hello” and “world”).

In the first two cases there are two tokens after the “=”,  “   Hello” (with three blanks before the “H”) and “world”; in the third case the two tokens are “Hello” and “world”(in each case without leading or trailing blanks).

On the other hand, the statement

AMPSET &1 = "   Hello  world   "

has only one token after the “=”, “   Hello world   ”.

Upper and Lower Case

By default the AMPSET command converts everything after the “=” to upper case. Thus the first of our cases above,

AMPSET &1 =    Hello world   

sets &1 to “   HELLO WORLD”, as does the second case. The third case sets &1 to “HELLO WORLD”.

To capture lower case strings you must enclose the string in single quotes. Thus:

AMPSET &1 = '   Hello  world   '

sets &1 to “   Hello world   ” (with three leading and three trailing blanks).

Getting Single and Double Quotes into & Variables

Note that if the single quote comes after a double quote it is regarded as a “literal” single quote and does not turn the AMPSET command into “save lower case” mode. Thus

AMPSET &1 = "'   Hello  world   '"

sets &1 to “'   HELLO WORLD   '” (with a single quote before three leading blanks and a single quote after three trailing blanks).

It is possible to get double quotes into an amper variable by including them in a single-quoted string in an AMPSET command:

AMPSET &1 = '"   Hello  world   "'

sets &1 to  “"   Hello world   "” ( "  HELLO WORLD   '” (with a double quote before three leading blanks and a double quote after three trailing blanks).

Setting &Variables within Rules

To set an amper variable within a rule you must work around the interactions between the AMPSET command’s syntax and that of the /E language.

Thus if you write

IF      ?MESSAGE = "   Hello world   "
AND     AMPSET &1 = ?MESSAGE

this induces the command

AMPSET &1 =    Hello world   

which sets &1 to “   HELLO WORLD”.

The statements

IF      ?MESSAGE = "   Hello world   "
AND     AMPSET &1 = '?MESSAGE'

might be thought to create a command with a single-quoted string. What they actually do is to induce the command:

AMPSET &1 = '   Hello world   

which does not have matching quotes. It sets &1 to “'   HELLO WORLD”.

If you happen to be using a local variable with fewer than 8 characters, the result is even worse. Thus

IF      ?MESS = "   Hello world   "
AND     AMPSET &1 = '?MESS'

causes the command interpreter to receive the command:

AMPSET &1 = '?MESS'

which sets &1 to “?MESS”!

These particular problems can be resolved by leaving at least one blank after ?MESSAGE before the double quote. Thus

IF      ?MESSAGE = "   Hello world"
AND     AMPSET &1 = '?MESSAGE   '

causes the command interpreter to receive

AMPSET &1 = '   Hello world   '

and so to set &1 to “   Hello world   ”.

Although this succeeds in getting lower case into an amper variable it leaves at least one trailing blank on the amper variable. This could be removed by using a SQUEEZE command.

A Better Approach to Setting &Variables from Rules

A better solution is not to put a trailing blank into the & variable in the first place. To do this we need to omit quotes from the AMPSET command and instead arrange for ?MESSAGE to begin and end with a single quote. This can most neatly be done by having a set called QUOTE with two elements, SINGLE and DOUBLE, whose descriptions are ' and " respectively. The code is then:

IF      ?SQUOTE = QUOTE(SINGLE,D)
AND     ?MESSAGE = "Hello world"
AND     ?QMESS = '?SQUOTE?MESSAGE?SQUOTE'
AND     AMPSET &1 = ?QMESS

This induces the command

AMPSET &1 = 'Hello world'

which sets &1 to “Hello world”.

The Best Approach to Setting &Variables from Rules

Finally, having gone to such trouble to set an amper variable to a literal character string, we might note that there is a much easier solution (at least so long as we do not require trailing blanks): to set the amper variable to a set or table element which contains the desired character string:

AND     AMPSET &1 = MYSET(MESSAGE,D)

where MYSET(MESSAGE,D) = Hello world.


Back                                Next
Comments