Scripting 5.1. Variables

A variable is a location in your computer’s memory that has a name and where you can store something, as explained in S3.2 and S3.4. In a Praat script, you can store numbers and texts, i.e. you can use numeric variables and string variables.

## Numeric variables

Numeric variables can hold 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 use numeric variables in your script like this:

`length = 10
` `Axes: 0, 100, 0, 100
` `Draw line: 0, 50, length, 50
` `Draw inner box
`

This draws a line in the Picture window from position (0, 50) to position (`length`, 50). In the first line, you assign the value 10 to the variable called `length`, and in the third line you use the value of `length` as the third argument to the command Draw line....

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

## String variables

You use string variables, which contain text, as follows:

`title$ = “Dutch nasal place assimilation”
` `Text top: “no”, title$
` `Draw inner box
`

This writes the text “Dutch nasal place assimilation” above your drawing.

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

## Making numeric variables visible

You can write the content of numeric variables directly to the info window:

`x = 2.0
` `root = sqrt (x)
` `writeInfoLine: “The square root of ”, x, “ is ”, root, “.”
`

When you try this out, this will write the following text to the Info window:

`The square root of 2 is 1.4142135623730951.
`

You can fix the number of digits after the decimal point by use of the `fixed$` function:

`x = 2.0
` `root = sqrt (x)
` `writeInfoLine: “The square root of ”, fixed$ (x, 3), “ is approximately ”, fixed$ (root, 3), “.”
` `=>`
`The square root of 2.000 is approximately 1.414.
`

By using 0 decimal digits, you round to whole values:

`root = sqrt (2)
` `writeInfoLine: “The square root of 2 is very approximately ”, fixed$ (root, 0), “.”
` `=>`
`The square root of 2 is very approximately 1.
`

By using the `percent$` function, you give the result in a percent format:

`jitter = 0.0156789
` `writeInfoLine: “The jitter is ”, percent$ (jitter, 3), “.”
` `=>`
`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
` `writeInfoLine: “The jitter is ”, percent$ (jitter, 3), “.”
` `=>`
`The jitter is 0.00002%.
`

## Predefined variables

All of the variables you saw earlier in this tutorial were defined at the first moment a value was assigned to them. Some variables, however, are already defined implicitly at the start of your script.

Some predefined numeric variables are `macintosh`, `windows`, and `unix`, which are 1 if your edition of Praat was built for the Macintosh, Windows, or Unix platform (respectively), and which are otherwise zero. Likewise, we have `praat_32bit` and `praat_64bit`, of which one is 1 and the other 0, depending on whether your edition of Praat was built for 32-bit or 64-bit computers. More precisely, we have `praat_intel32`, `praat_intel64`, `praat_arm64`, `praat_s390x`, and `praat_armv7`, one of which is 1 (and the others 0) depending on whether the type of processor chip that your edition of Praat was made for is 32-bit Intel (= x86, i386, i686), or 64-bit Intel (= AMD64), or ARM64 (= Aarch64), or s390x (LinuxONE), or ARMv7 (smaller Raspberry Pi). Another predefined numeric variable is `praatVersion`, which is something like 6443.

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

A disadvantage of predefined variables is that they can be changed, in which case they take on a different meaning. For this reason you are advised to use functions instead (if they exist), because these always have the same meaning:

  • `appVersion`() instead of `praatVersion`
  • `appVersion$`() instead of `praatVersion$`

## Functions that handle variables

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

`variableExists (variableName$)
`

as in

`fgh = 567
` `assert variableExists ("fgh")
` `assert not variableExists ("jhfwbfejfgcds")
`

### Links to this page

- Scripting
- Scripting 3.3. Numeric queries
- Scripting 5. Language elements reference