Workshop
Generating Output

Introduction

In real life, most of the time, output commands are applied to write out the content of variables. But variables are still one session away, so we'll start practicing with literal numbers and strings. After that, it is easy to extend your dewy new output skills to variables, as you'll see in the next session.

Preparation

Start Praat and open a new Praat script.

Exercise

First, acquaint yourself with the output commands. There is no help from command history, you have to write the code yourself. These are the relevant commands:

writeInfo: writeInfoLine: appendInfo: appendInfoLine:

Play around with the commands, add some arguments (strings with double quotes, numbers without, separated by comma), and see what happens.

Now, try to accomplish the following output:

Line1 Word1 Word2
Line2 Word3 Word4 Word5 Word6

using this sequence of commands:

writeInfo: appendInfo: appendInfoLine: appendInfo: appendInfo:

Easy? So, try to meet this special requirement: The last statement has 3 arguments, one of which is numerical.

  • Solution
    writeInfo: "Line1" appendInfo: " Word1" appendInfoLine: " Word2" appendInfo: "Line2 Word3" appendInfo: " Word", 4, " Word5 Word6"

    or

    writeInfo: "Line1" appendInfo: " Word1" appendInfoLine: " Word2" appendInfo: "Line2 Word3 Word4" appendInfo: " Word", 5, " Word6"

    or...

Next, experiment with tabbed output. Take any output command, add some arguments and throw in a few tab$-items. Change font size (Font-menu in Praat Info) and see what happens to the column alignment.

When you're ready, set font size to 14 and produce the following output. Don't use spaces!

a      b      c
1      2      3
10     20     30
100    200    300
1000   2000   3000
  • Solution
    writeInfoLine: "a", tab$, tab$, "b", tab$, tab$, "c" appendInfoLine: 1, tab$, tab$, 2, tab$, tab$, 3 appendInfoLine: 10, tab$, tab$, 20, tab$, tab$, 30 appendInfoLine: 100, tab$, tab$, 200, tab$, tab$, 300 appendInfoLine: 1000, tab$, 2000, tab$, 3000

If you play around and try this with larger numbers, you'll see that it's not really feasible to assemble neat tables with the help of tab$. Rather, tab$ is intended to generate lists of tab separated values, which could be used e.g. as input for spreadsheets.

Now, we are going to exploit that output commands write out the result if they are fed with a formula:

writeInfo: 2 + 4

writes 6 to Praat Info. Most of the time, as you will see, results of formulas in scripts are assigned to variables, but passing them directly to Praat Info is a simple and effective way to acquaint yourself with operators, operator precedence and other aspects of formulas.

The basic arithmetic operators for formulas are + (addition), - (subtraction), * (multiplication), and / (division).

writeInfoLine: "basic arithmetics:" appendInfoLine: 9+3 appendInfoLine: 9-3 appendInfoLine: 9*3 appendInfoLine: 9/3

When you run this script you get:

basic arithmetics:
12
6
27
3

If no parentheses are used, multiplication and division are calculated before addition and subtraction and formulas are calculated from left to right. Look at the following script—don't run it ;-)   What results do you expect?

writeInfoLine: "operator precedence and parentheses:" appendInfoLine: 2+1*9 appendInfoLine: (2+1)*9 appendInfoLine: 2-1+4 appendInfoLine: 2-(1+4)
  • Answer
    operator precedence and parentheses:
    11
    27
    5
    -3

To learn more about operators and formulas, read the Praat tutorial on operators and put together formulas as complex as you like to become familiar with script controlled arithmetics.

To conclude this workshop, let's test newline$. Try to accomplish the following output with one line of code:

Multiplication
2 * 8 = 16

Division
16 / 2 = 8

Make sure that the results (16 and 8) are calculated by Praat!

  • Solution
    writeInfo: "Multiplication", newline$, "2 * 8 = ", 2 * 8, newline$, newline$, "Division", newline$, "16 / 2 = ", 16 / 2

© Jörg Mayer

Last modified: