Scripting 5.1. Variables

In a Praat script, you can use numeric variables as well as string variables.

Numeric variables

Numeric variables contain integer numbers between -1,000,000,000,000,000 and +1,000,000,000,000,000 or real numbers between -10308 and +10308. The smallest numbers lie near -10-308 and +10-308.

You can use numeric variables in your script:

variable = formula
evaluates a numeric formula and assign the result to a variable.

Example:

length = 10
Draw line... 0 length 1 1

Names of numeric variables must start with a lower-case letter, optionally followed by a sequence of letters, digits, and underscores.

String variables

You can also use string variables, which contain text:

title$ = "Dutch nasal place assimilation"

As in the programming language Basic, the names of string variables end in a dollar sign.

Variable substitution

Existing variables are substituted when put between quotes:

x = 99
x2 = x * x
echo The square of 'x' is 'x2'.

This will write the following text to the Info window:

The square of 99 is 9801.

You can reduce the number of digits after the decimal point by use of the colon:

root = sqrt (2)
echo The square root of 2 is approximately 'root:3'.

This will write the following text to the Info window:

The square root of 2 is approximately 1.414.

By using ":0", you round to whole values:

root = sqrt (2)
echo The square root of 2 is very approximately 'root:0'.

This will write the following text to the Info window:

The square root of 2 is very approximately 1.

By using ":3%", you give the result in a percent format:

jitter = 0.0156789
echo The jitter is 'jitter:3%'.

This will write the following text to the Info window:

The jitter is 1.568%.

The number 0, however, will always be written as 0, and for small numbers the number of significant digits will never be less than 1:

jitter = 0.000000156789
echo The jitter is 'jitter:3%'.

This will write the following text to the Info window:

The jitter is 0.00002%.

Some predefined numeric variables are macintosh, windows, and unix, which are 1 if the script is running on a Macintosh, Windows, or Unix platform (respectively), and which are otherwise zero. Another one is praatVersion, which is e.g. 5315 for the current version of Praat.

Some predefined string variables are newline$, tab$, and shellDirectory$. The last one specifies the directory that was the default directory when Praat started up; you can use it in scripts that run from the Unix or DOS command line. Likewise, there exist the predefined string variables homeDirectory$, preferencesDirectory$, and temporaryDirectory$. These three refer to your home directory (which is where you log in), the Praat preferences directory, and a directory for saving temporary files; if you want to know what they are on your computer, try to echo them in a script window. The variable defaultDirectory$ is available for formulas in scripts; it is the directory that contains the script file. Finally, we have praatVersion$, which is "5.3.15" for the current version of Praat.

To check whether a variable exists, you can use the function

variableExists (variableName$)

Links to this page


© ppgb, August 9, 2009