next up previous
Next: 6. Controlling the Output with Up: A Tutorial on the Previous: 4. Compound Macro Statements

5. Text Manipulations

Ten text registers, each holding up to 124 characters, allow storing of any kind of text information. The registers t1-t8 are general purpose read/write registers with no predefined special usage, while text registers t0 and t9 have some special properties.

Text registers are written into with the ``set register'' command (e.g. ``~t5=this is a text''), and are read by means of the text register substitution (e.g. ``%t5%''). Special variants of the substitution keys allow accessing of substrings by limiting the number of characters or skipping initial characters. Using the same example as above, ``%t5.2%'' will be replaced by the string ``th'', and ``%t5.-2%'' becomes ``is is a text''. Try to analyze the following sequence (hint: %1% contains a matrix identifier):

    ~t5=%1%
    ~t9=%t5.2%
    ~?t9=md
    ~t5=mo%t5.-2%
    ~?t9=mo
    ~t5=md%t5.-2%

As illustrated above, text registers can be compared with another text, using conditionals. The < and > comparisons are based on the standard ASCII collating sequence.

Register t0 behaves exactly as the other text registers, but its contents is always equivalent with the current macro parameters, i.e. the string ``%1% %2% %3% ...''. Using %t0% to manipulate the entire set of macro parameters has also the advantage that even more than 9 parameters can be handled correctly. Shifting the parameters with the ~% command will, of course, also modify the contents of t0. Modifying t0, on the other hand, also implies changing of the macro parameters. In the following sequence, the macro tests if it has been called without parameters, in which case it ``installs'' a set of default parameters:

    ~?t0=
    ~t0=ul1 1.0 yes
    ~/Using macro parameters: %1% %2% %3%

This special property of t0 can also be used to split a given text into fields or words, by assigning the text to t0 and accessing the words as %1%, %2%, etc. If this is done, the ``real'' macro parameters can be stored away into another register and reinstalled later on, as shown in the following sequence:

    ~t9=%t0%
    ~t0=~*Enter: First and family name=
    ~t1=%1%
    ~t2=%2%
    ~t0=%t9%
    ~/First name: %t1%     Family name:%t2%

The above example also shows how a user can be prompted to enter a text which is stored in a text register. This is useful in circumstances where the same parameter is to be used in different places in the macro.

Register t9 is the only global register. This means that its value may be modified by a macro running at a lower level. Hence, t9 is predestined to be used to pass return parameters back from a sub-level macro.

By the way, don't forget that text registers can of course also serve to store numerical values! This is often useful when running short of numerical registers.



Exercise 5.1:
Write a sequence of macro commands which prompts the user with the question ``Do you really want to proceed?'' and proceeds only if the user enters a ``yes'', or any other string starting with the letter ``y''. The macro stops if a ``no'' (or another string starting with ``n'') is entered. Any other answer will cause the question to be asked again.



Exercise 5.2:
Write a macro sequence to extract a variable substring from text register t5, the starting position is contained in the x register and the length of the substring is given in the y register. The resulting substring is stored in t9.


next up previous
Next: 6. Controlling the Output with Up: A Tutorial on the Previous: 4. Compound Macro Statements

Heinz Spiess, EMME/2 Support Center
Fri Sep 27 13:04:28 MET DST 1996