Home Web Design Programming Fairlight CMI Soap Box Downloads Links Biography About... Site Map

The Holmes Page The Macro Operator "&"

CA-Clipper Opportunities Tips and Tricks Networking Internal Errors Source Code CA-VO


The macro operator has been around for quite a few versions of CA-Clipper. In fact it was also available in dBase. It has been used for some fairly obscure (read: bad) source code tricks. But then, in the "good old days" there were fewer alternatives! 
For the most part, the macro operator has been replaced by code blocks (see The Evolution of Code Blocks). Nevertheless, it is useful to understand the macro compiler, if only to understand the "legacy" code that has to be maintained. 
Jumping right in, consider the following source code:
01  X := 10
02  Y := 'X'
03  ? &Y.                    // Displays the number 10.
The variable X is assigned the number 10, while the variable Y is assigned the character string 'X', which is the name of the first variable. 
Line 3 shows the macro operator at work. The ampersand (&) and the period surround the variable Y like a pair of parentheses. This line of code causes the number 10 to appear on the screen. 
Looking closer at this, you can see that the variable X contains the number 10, so why does line 3 of the sample print 10? Using a high school mathematics technique, substitution, some light can be shone on this. 
On line 2, Y is defined as 'X', so substituting this into line 3 gives:
03  ? &'X'. 
Now, use the ampersand and the period to "cancel out" the quotes:
03  ? --X-- 
Which leaves this:
03  ? X 
This line prints the value of X, which is 10. 
Note  The CA-Clipper documentation indicates that the period is optional when used with the macro operator, but I like to include it because of the way that it reminds me to "cancel the quotes". 
Here is another example:
01  X := 10
02  Y := 'X + 1'
03  ? &Y.                    // Displays 11.
Here is the first step, substitution:
03 ? &'X + 1'. 
The macro operator and period cancel the quotes:
03 ? --X + 1-- 
Which leaves this code:
03 ? X + 1 
Another example:
01  X := -10
02  Y := '(abs(X) / 2) + 1'
03  ? &Y.                    // Displays 6.
You can see that the macro operator strips away the quotes which leaves a snippet of code to be executed. Keep in mind that all of this happens at run-time, which makes it very dynamic. One neat use might be to create a kind of calculator, or formula evaluator, that determines the result of something that the user types in. 
The macro operator has a few limitations, such as the fact that variables referenced inside the string (X in the samples above) cannot be local variables. Also, functions used in the macro string (abs() in the sample) must be explicitly linked into the program (usually by simply including external ABS at the top of the source code file. 
Nevertheless, the macro operator is still used occasionally, although it has been mostly superceded by code blocks.

Home Web Design Programming Fairlight CMI Soap Box Downloads Links Biography About... Site Map

Site Map Send comments about this site to Greg at gregh@ghservices.com
All pages copyright © 1996-1999 GH Services™   Created 1997/06/06   Last updated 1999/09/30
All trademarks contained herein are the property of their respective owners