Omni Systems, Inc.

  

Mif2Go User's Guide, Version 55

  

Valid HTML 4.01!

 

Made with Mif2Go

28 Working with macros > 28.6 Using expressions in macros > 28.6.3 Displaying expression results in output


28.6.3 Displaying expression results in output

In general, Mif2Go macro expressions produce output. The exceptions are as follows:

assignments, where much of the time you are going to use the assigned value later (see §28.3.2 Assigning values to macro variables)

control statements, which have no obvious meaning of their own (see §28.6.4 Using control structures in expressions).

To display (that is, to include in HTML output) the result of evaluating an expression, enclose the expression in parentheses, as follows:

<$(... expr ...)>

You can also specify a display format to use, with as plus a C-language-style format string:

<$(... expr ...) as format-string>

A format string starts with “%” (percent sign) and is composed as follows, where any component enclosed in [] is optional:

%[flag(s)][width][.precision]format-code

The components of the format string can have any of the values listed in Table 28-5.

Integer precision

Suppose you wish to display the integer value of user variable $$myint, which you have set to internal value 5:

<$$myint = 5>

When you use a format string to display the value, the default integer precision is 1, as you can determine by comparing the results of the following expressions:

<$$myint as %0d>

<$$myint as %0.1d>

<$$myint as %0.3d>

The first two yield identical results, 5, while the third yields 005. However, when you do not use the “as %” construct, there is no precision; you get the internal string representation, which has three digits, unless you initialized it otherwise.

Table 28-5 Format components for displaying expression results

Component

Value

Effect on output

flag

-

The result is left justified in the display field (the default is right justified)

+

The sign of the result is displayed (the default is to display the sign only for negative values)

(blank)

A blank is displayed for positive values, a minus sign for negative values

#

Hexadecimal result: displayed with the prefix 0X or 0x, depending on the format code

Fractional decimal result: the decimal point is displayed

Integer decimal result: no effect

width

(integer)

Minimum size of display field in characters

precision 

(integer)

Integer result: minimum number of digits displayed (the default is 1)

Fractional result: number of digits displayed after the decimal point

String result: maximum number of characters displayed (the default is the entire string)

format-code

 

 

c

The result is displayed as a character

d

The result is displayed as a decimal number

s

The result is displayed as a string of characters

x

The result is displayed as a hexadecimal number, with lowercase a through f

X

The result is displayed as a hexadecimal number, with uppercase A through F

Additional format options

For more information about C-language format strings and for additional components and format codes, see the following reference:

http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.12.html#printf

You can use any C-language format codes except those for floating-point values (e, f, g) or for pointers (p). It is best not to use the h or l (lowercase L) modifiers; however, if you ignore this advice, l is at least harmless.

As an example, this macro generates an ASCII table:

[Charset]

<$$cval = ' '>

<$_while ($$cval < '~')>\

<p><$$cval = ($$cval + 1) as %0.3d> \

0x<$$cval as %02X> \

<$$cval as %c></p>

Hexadecimal output

In an expression, hexadecimal numbers beginning with 0x or 0X are understood as numeric values. But if you have a hexadecimal number stored in a variable, and try to display it like this:

<$$myvar as %d> (or “as %c”, or even “as %x”)

you get 0 (zero) as output—for any hexadecimal number. Use the default (“as %s”), or just plain <$$myvar>.



28 Working with macros > 28.6 Using expressions in macros > 28.6.3 Displaying expression results in output