Go to the first, previous, next, last section, table of contents.


Fun With ]print

The ]print function is the indispensable all-in-one Swiss Army Knife of string-construction functions. (If you've ever played with C, you'll recognize is as just C's printf() function in MUF clothing.)

Stack:
[ "Test!" | ]print
Stack: "Test!"
pop   [ "I am %d. You are %d." 2 6 | ]print
Stack: "I am 2. You are 6."
pop   [ "Call me %s." "Ishmael" | ]print
Stack: "Call me Ishmael."

]print takes a block consisting of a format string followed by any needed operands, and returns a new string consisting of the format string with data values substituted into it at appropriate spots.

How does ]print know where to do the substitutions? Simple! It scans the format string looking for % characters. The % character tells it where to substitute, and the following letter tells it what sort of substitution is intended:

%s  a string ("String")
%d  an integer ("Decimal number" -- but no decimal point!)
%f  a Floating point point number.  Print like "123.456"
%%  put a single '%' here in the result string.

(]print knows how to do other sorts of substitutions, and can be told exotic things such as how many digits to print after the decimal point, but we'll save those for the intermediate MUF programming chapter. The above three types of substitution suffice for 90% of normal coding.)

The values to be substituted follow the format string, in the same order as their substitution points.

That's really all the essentials on ]print! You may want to play with it awhile to get used to using it.


Go to the first, previous, next, last section, table of contents.