|
|||||
|
|||||
Related Syntax | |||||
Format items |
Format items are a means of inserting data into a literal string. They have a wide variety of uses:
Format items can be divided into two classes based on their syntax:
The syntax of a static format item is as follows:
escape
character; by default, "%".
Static format items are
macro
expansion.
The format items that represent white space ("t", "n", "_") can take an "s" format modifier. This causes only one of that character to be output, even if two or more of the format items are output in succession. This is useful when building strings dynamically.
process output "First line%sn" output "%snSecond line %sn"
Dynamic format items are generally used with the format operator, as in:
escape
character; by default, "%"
Note that format items do not accept expressions or literal strings. To format the result of an expression, use the format item in quotes, the %
operator, followed by the expression or quoted string.
Literal string example:
; pad-quote-string.xom ; "8fg" pads the quoted string "foo" with spaces to a width of 8 process output "8fg" % "foo" || "end" ; Output: "foo END"
Two kinds of format strings are supported, format instructions and templates. A format instruction consists of a format command preceded by one or more format modifiers. A template consists of "<", a set of template characters, and ">".
The example format.xom uses format instructions:
; format.xom import "ombcd.xmd" unprefixed process local bcd foo initial {233.33} local stream bar initial {"'Twas brillig and the slithy toves"} output "Foo padded to right to 8 digits: [" || "8fd" % foo || "]%n" output "Bar uppercased: " || "ug" % bar || "%n" ; Output: Foo padded to right to 8 digits: [233.33 ] ; Bar uppercased: 'TWAS BRILLIG AND THE SLITHY TOVES
In format.xom, we have two format instructions:
"8fd" % foo
, in which "8f" is the format modifier, "d" is the format command, and "%" is the dynamic format item.
"ug" % bar
, in which "u" is the format modifier, "g" is the format command, and "%" is the dynamic format item.
At the end of the line containing each of the two format instructions, there is also a static format item, "%n", to insert a newline character.
The format string varies according to the type of the variable.
To format the values of specific data types using dynamic format items, see:
The format commands used in dynamic format items, and the types they are used with, are:
macro
arguments
Related Syntax escape |