Create ASCII-Output or Scripts
FormatWrite |
The format write object is used to create ASCII-files of any format or a shell script which
is executed. A common example is to create a xmgrace graphic file, an HTML file
or a TeX file containing data calclculated before.
Fig. 1: Property dialog
Figure 1 shows format writer's property dialog. A template of the file to write
is to be given in the lower part of the dialog. This template is scanned for variable names
embedded within two control characters and the variable name with the control characters
is replaced with the variable's contents.
If the templated doesn't contain control
characters, the only operation of the format write object is to write this
template into a file. The mode buttons determine how the file is written:
- Write into File
- The template is written into the file specified with "Output filename". If "Append" is
not checked, the file is truncated if it already existed, otherwise the template is appended.
The output variable Exit code contains 1 if the write succeeded and
0 an error occurred.
- Exec as Script
- The template is written into a file with a temporary file with the execution
bit set. The file is executed afterwards. For this to work properly the first line of the
file should look like
#!/bin/sh
or something similar, to tell the system which interpreter to call. The temporary file
is deleted after execution. The output variable Exit code contains the return
code of the script.
Writing arrays
If the name of an array variable is given within control characters the replaced value
is the length of the array. For writing the contents of array variables a control sequence
${ ... }$ must be used. This works in the following manner: The part of the
enclosed in the control sequence is scanned for array variables. For each element of the
array variables the enclosed part is repeated and the array variable names are
replaced with the element at the current line. The enclosed part can also contain variables
that are no arrays, where the replaced value is always the same.
Example:
The sequence
${$array1$, $array2$
}$
can be used to generate an ASCII file with two columns that are separated by a ","
(the variables "array1" and "array2" are ment to be integer or floating point arrays). The
generate file would for example look like that:
24.333, 78.273
38.334, 38.766
... , ...
and so on. In this example it is important that the "}$" appears on a new line, as the line
feed is not generated automatically and must be contained in the enclosed part. Something
like
${$array1$, $array2$; }$
would lead to an output file like
24.333, 78.273; 38.334, 38.766; ... , ...; ...
that only contains one single line.
All array variables within a single control sequence should have the same length,
otherwise the length of the shortest array variable is taken as replication count. The
${ ... }$ can also be nested so that for each outer control sequence replacements
all inner sequence replacements are done (no idea whether this can be useful :-)).
The Vimms User Manual