fprint: Print a line into a parameter

Package: language

Usage

print  expr [expr ...]
fprint param expr [expr ...]
printf format [arg ...]

Parameters

expr
Any expression, the string value of which is to be printed.
param
The fprint call will deposit the output string in the value field of this parameter.
format
A C-like format specification string containing plain characters, which are simply copied into the output string, and conversion specifications, each of which causes conversion and printing of zero or more args.
arg
Any expression, variable, parameter value or constant to be used in the format string's conversion specifications. One arg is required for each conversion specification.

Description

The print and fprint commands format a line of text and write it to either the standard output or in the case of fprint, the p_value field of the named parameter. The output is free format although spaces may be specifically inserted (as quoted string constants) to make the output easier to read. One space is automatically inserted after each numeric argument; this can be defeated by coercing the argument to a string with the str intrinsic function. A newline is automatically output at the end of the output line. I/O redirection may be used with print or printf to write to a file or through a pipe to another task.

The printf command allows more control over the output format and can convert arguments for printing as another type. The format string contains plain text characters and format specifications as well as escape sequences for printing tabs, newlines, etc. Unlike the other two commands, spaces and newlines must be explicitly given in the format string.

A format specification has the form "%[W][.D]Cn", where W is the field width, D is the number of decimal places or the number of digits of precision, C is the format code, and N is radix character for format code "r" only. The W and D fields are optional. The format codes C are as follows:

b    boolean (YES or NO)
c    single character (c or '\c' or '\0nnn')
d    decimal integer
e    exponential format (D specifies the precision)
f    fixed format (D specifies the number of decimal places)
g    general format (D specifies the precision)
h    hms format (hh:mm:ss.ss, D = no. decimal places)
m    minutes, seconds (or hours, minutes) (mm:ss.ss)
o    octal integer
rN   convert integer in any radix N
s    string (D field specifies max chars to print)
t    advance To column given as field W
u    unsigned decimal integer
w    output the number of spaces given by field W
x    hexadecimal integer
z    complex format (r,r) (D = precision)

Conventions for W (field width) specification:

W =  n      right justify in field of N characters, blank fill
    -n      left justify in field of N characters, blank fill
    0n      zero fill at left (only if right justified)
absent, 0   use as much space as needed (D field sets precision)

Escape sequences (e.g. "\n" for newline):

     formfeed
\n      newline (crlf)
\r      carriage return
\t      tab
\"      string delimiter character
\'      character constant delimiter character
\\      backslash character
\nnn    octal value of character

Compute mode (a parenthesized argument list) is recommended for this task to avoid surprises.

Examples

1. Print the name of the current terminal.

cl> print ("terminal = ", envget ("terminal"))

2. Output a blank line on the standard output, e.g., in a script.

print ("")

3. Format a command and send it to the host system. In this example, "fname" is a string valued parameter.

cl> print ("!ls -l ", fname) | cl

4. Write to a file.

for (x=1.;  x < 1E5;  x *= 10)
    print ("the sqrt of ", x, "is ", sqrt(x), >> "output")

5. Print a formatted string.

cl> printf ("pi = %.6f\n", 2*atan2(1.0,0.0))
pi = 3.141593
cl> printf ("RA = %h  DEC = %m\nExptime = %8.2f\n",ra,dec,etime)
RA = 18:32:33.5 DEC = 23:45.2   Exptime =     1.57

6. Print to a parameter. Note that fprint allows you to create a formatted string, whereas the scan() example requires a struct parameter.

cl> x = 3.14159
cl> fprint (s1, "pi = ", x)
cl> = s1
pi = 3.14159

or

cl> printf ("pi = %g\n", x) | scan (line)

Bugs

The fprint task is not very useful since the same thing can be accomplished by string concatenation and assignment.

See also

scan, scanf, fscan, fscanf, strings