This plugin provides printf(3)
-style string formatting procedures
for Praat.
These procedures take a format string, and an optional series of variables, which will get formatted according to the format specification.
Since calls to the provided procedures will take a variable number of arguments,
and Praat does not allow procedures to do this, the procedures provided by this
plugin make use of the @
procedure from varargs
. Calling them through any
other mean is not supported, and may result in unexpected behaviour.
The format string will be parsed for the presence of format prototypes, and each of them will be replaced by a formatted version of the appropriate variable.
Format prototypes are identified by a starting percent character (%
) followed
by a format specifier, which is a single character. The character specifies the
way in which the variable should be interpreted. Valid specifiers are the
following:
%% a percent sign
%s a string
%d a signed integer, in decimal
%i a synonym for %d
%u an unsigned integer, in decimal
%o an unsigned integer, in octal
%x an unsigned integer, in hexadecimal
%b an unsigned integer, in binary
%e a floating-point number, in scientific notation
%g a floating-point number, in %e or %f notation
%a a floating-point number, in hexadecimal
%f a floating-point number, in fixed decimal notation
%F a synonym for %f
%X like %x, but using upper-case letters
%E like %e, but using an upper-case "E"
%G like %g, but with an upper-case "E" (if applicable)
%B like %b, but using an upper-case "B" with the # flag
%A like %a, but using upper-case letters
Additionally, limited support is given for
%c a character with the given number
As an example:
Between the %
character and the specifier, several additional attributes
may be given to control how the specifier is to be interpreted.
In order, these are:
Normally, the variables to format are read in order into each of the specifier characters, but this can be modified by providing an explicit parameter index.
Indices are given by a number followed by a dollar character ($
), and the
variables are indexed from 1
. As an example:
After the parameter index, one or more of the following can be provided:
space prefix non-negative number with a space
+ prefix non-negative number with a plus sign
- left-justify within the field
0 use zeros, not spaces, to right-justify
# ensure the leading "0" for any octal,
prefix non-zero hexadecimal with "0x" or "0X",
prefix non-zero binary with "0b" or "0B"
These result in the following examples:
If the +
and ` ` flags are given, the space is ignored:
Arguments are usually formatted to be only as wide as required to display
the given value. This can be overriden by specifying a field width, or by
getting the width from the next argument (with *
):
If a field width obtained through *
is negative, it has the same effect
as the -
flag: left-justification.
The meaning of this attribute depends on the specifier.
A precision (for numeric conversions) or a maximum width (for string
conversions) can be specified with a .
followed by a number. For
floating-point formats except %g
and %G
, this specifies how many
places after the decimal point are shown (the default being 6). For example:
For %g
and %G
, the precision specifies the maximum number of digits to
show, including those on both sides of the decimal point; for example:
For integer conversions, specifying a precision implies that the output
of the number itself should be zero-padded to this width, where the 0
flag
is ignored:
For string conversions, specifying a precision truncates the string to fit the specified width:
You can also get the precision from the next argument using .*
:
If a precision obtained by this method is negative, it is discarded.
If the #
flag is given or an octal number, the precision may be increased
in order to accommodate the leading 0
:
[Of limited support in Praat implementation]
For convenience, this plugin includes two scripts that provide access to the procedures of the same name. They work in exactly the same way as the methods, described below.
A script version of @printf
. The script takes two string arguments:
the first argument is the format string, and the second is a comma-separated
list of arguments to be passed to the internal parser. Because this string
will be parsed, predefined variable names (eg. undefined
) are acceptable.
A script version of @fprintf
. The script takes three string
arguments: the first argument is the name of the file to write to, the
second is the format string, and the last is a comma-separated list of
arguments to be passed to the internal parser. Because this string will
be parsed, predefined variable names (eg. undefined
) are acceptable.
printf.proc
Format the given variables according to the format string, and print the
results to the info window. This procedure is entirely equivalent to manually
printing the results of a call to @sprintf
with, for example,
appendInfo()
.
Format the given variables according to the format string, and store it in
the sprintf.return$
variable. Unlike with @printf
, nothing is
printed to the info window after calling this procedure.
Note that this procedure does not automatically append a newline. In order to do that, it must be a part of the format string.
Format the given variables according to the format string, and append the
results to the file whose name is passed as the first argument. This procedure
is entirely equivalent to manually printing the results of a call to
@sprintf
with, for example, appendFile()
.
If the file does not exist when the procedure is called, it will be created.
The plugin attempts to outsource the formatting to helper scripts written in
Perl using the runSystem()
function. In systems where Perl is not available,
a pure-Praat fallback method is provided.
The availbility of Perl is checked automatically when the procedures are
loaded (ie. with include
), but finer control can be achieved by modifying the
value of the printf.system
variable at runtime.
Except in those cases where Perl uses a different locale information, the output of the provided procedures should be the same regardless of what method is used.
Since the Perl helper scripts are executed as system commands, they cannot know
about the info window, and print directly to STDOUT. This would not normally be
a problem when Praat is run from a command line, but even in these cases, the
use of functions like info$()
, which read the info window (and not STDOUT)
would be different.
In order to maintain a predictable behaviour, the plugin by default captures
the output of the Perl scripts and passes it to the info window. However, this
requires a file I/O operation, which could be a problem. If this is not
desirable, and the effects outlined above are not important, this behaviour
can be altered by setting the printf.stdout
variable to a true value.
The implementation in Praat follows closely that made by Perl, and this page itself borrows heavily from the Perl documentation of the corresponding function, including the examples (except when adjusted to accommotate Praat particulars).