| 1 | <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 | <title>Scripting 3.2. Numeric variables</title> |
| 3 | <style> |
| 4 | td { padding-left: 5pt; padding-right: 5pt; } |
| 5 | th { padding-left: 5pt; padding-right: 5pt; } |
| 6 | code { white-space: pre-wrap; } |
| 7 | dd { white-space: pre-wrap; } |
| 8 | </style> |
| 9 | </head><body bgcolor="#FFFFFF"> |
| 10 | |
| 11 | <table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor="#CCCC00"><table border=4 cellpadding=9><tr><td align=middle bgcolor="#000000"><font face="Palatino,Times" size=6 color="#999900"><b> |
| 12 | Scripting 3.2. Numeric variables |
| 13 | </b></font></table></table> |
| 14 | <p>In any general procedural programming language you can work with <i>variables</i>, which are places in your computer's memory where you can store a number or anything else.</p> |
| 15 | <p>For instance, you could put the number 3.1 into the variable <code><font size=+1><i>b</i></font></code> in the following way:</p> |
| 16 | <code> b = 3.1<br></code> |
| 17 | <p>This statement is called as <i>assignment</i>, i.e., you <i>assign</i> the <i>value</i> 3.1 to the <i>variable</i> <i><code><font size=+1>b</font></code></i>. We read this statement aloud as “<i><code><font size=+1>b</font></code></i> becomes 3.1” (or “<i><code><font size=+1>b</font></code></i> gets 3.1”, but not “<i><code><font size=+1>b</font></code></i> is 3.1”). What this means is that after this statement, the memory location <i><code><font size=+1>b</font></code></i> <i>contains</i> the numeric value (number) 3.1.</p> |
| 18 | <p>You can regard a variable as a box: you put the value 3.1 into the box named <i><code><font size=+1>b</font></code></i>. Or you can regard a variable as a house: the house is called <i><code><font size=+1>b</font></code></i> and now the family “3.1” is living there. Or you can regard it as any other storage location.</p> |
| 19 | <p>To see what value a variable contains (what’s in the box, or who lives in the house), you can use the <b><code><font size=+1>writeInfoLine</font></code></b> function:</p> |
| 20 | <code> b = 3.1<br></code> |
| 21 | <code> writeInfoLine: “The value is ”, b, “.”<br></code> |
| 22 | <p>This will put the text “<code><font size=+1>The value is 3.1.</font></code>” into the Info window, as you are invited to verify.</p> |
| 23 | <p>A variable is called a variable because it is <i>variable</i>, i.e. its value can change. Try the script</p> |
| 24 | <code> b = 3.1<br></code> |
| 25 | <code> b = 5.8<br></code> |
| 26 | <code> writeInfoLine: “The value is ”, b, “.”<br></code> |
| 27 | <p>You will see that <i><code><font size=+1>b</font></code></i> ends up having the value 5.8. The first line puts the value 3.1 there, but the second line replaces it with 5.8. It’s like taking the 3.1 out of the box and putting the 5.8 in its stead. Or the family 3.1 moves from the house, and the family called 5.8 moves in.</p> |
| 28 | <p>In an assignment, the part to the right of the “becomes” sign (the “=” sign) doesn’t have to be a number; it can be any <i>formula</i> that <i>evaluates</i> to a number. For instance, the script</p> |
| 29 | <code> b = 3.1 * 2<br></code> |
| 30 | <code> writeInfoLine: “The value is ”, b, “.”<br></code> |
| 31 | <p>puts the text “<code><font size=+1>The value is 6.2.</font></code>” into the Info window. This works because Praat handles the first line in the following way:</p> |
| 32 | <dl> |
| 33 | <dd style="position:relative;padding-left:1em;text-indent:-2em">1. the formula <code><font size=+1>3.1 * 2</font></code> is <i>evaluated</i> (i.e. its value is computed), and the result is 6.2.</dd> |
| 34 | <dd style="position:relative;padding-left:1em;text-indent:-2em">2. the value 6.2 is subsequently stored in the variable <code><font size=+1>b</font></code>.</dd> |
| 35 | </dl> |
| 36 | <p>After line 1 has been executed, the variable <i><code><font size=+1>b</font></code></i> just contains the value 6.2, nothing more; the variable <i><code><font size=+1>b</font></code></i> doesn’t remember that that value has been computed by multiplying 3.1 with 2.</p> |
| 37 | <p>Formulas can contain more things than numbers: they can also contain other variables:</p> |
| 38 | <code> b = 3.1<br></code> |
| 39 | <code> c = b * 2<br></code> |
| 40 | <code> writeInfoLine: “The value of b is ”, b, “, and the value of c is ”, c, “.”<br></code> |
| 41 | <p>In the first line, <i><code><font size=+1>b</font></code></i> gets the value 3.1. In the second line, the formula <code><font size=+1>b * 2</font></code> first has to be evaluated. Praat looks up the value of <i><code><font size=+1>b</font></code></i> (which is 3.1), so that it knows that the formula actually means <code><font size=+1>3.1 * 2</font></code>. Praat evaluates this formula and stores the result (namely the value 6.2) into the variable <i><code><font size=+1>c</font></code></i>, which will then contain nothing else than the value 6.2. The Info window thus reports “<code><font size=+1>The value of b is 3.1, and the value of c is 6.2.</font></code>”.</p> |
| 42 | <p>After these explanations, consider the following script:</p> |
| 43 | <code> b = 3.1<br></code> |
| 44 | <code> c = b * 2<br></code> |
| 45 | <code> b = 5.8<br></code> |
| 46 | <code> writeInfoLine: “The value of c is ”, c, “.”<br></code> |
| 47 | <p>Can you figure out what the Info will report? If you think it will report “<code><font size=+1>The value of c is 6.2.</font></code>”, then you are correct: after the first line, <i><code><font size=+1>b</font></code></i> contains the value 3.1; after the second line, the value of <i><code><font size=+1>c</font></code></i> is therefore 6.2, and nothing more; after line 3, the value of <i><code><font size=+1>b</font></code></i> has changed to 5.8, but the value of <i><code><font size=+1>c</font></code></i> hasn’t changed and is still 6.2.</p> |
| 48 | <p>If you thought that <i><code><font size=+1>c</font></code></i> would end up having the value 11.6, then you’re thinking in terms of a non-procedural language such as Prolog; you may have thought that the thing assigned to <i><code><font size=+1>c</font></code></i> in the second line is the whole <i>formula</i> <code><font size=+1>b * 2</font></code>, so that <i><code><font size=+1>c</font></code></i> changes when <i><code><font size=+1>b</font></code></i> changes. But this is not the case: the thing stored in <i><code><font size=+1>c</font></code></i> is just the <i>value</i> of the formula <code><font size=+1>b * 2</font></code> at that moment, which is 6.2, and <i><code><font size=+1>c</font></code></i> doesn’t remember how it got that value. If you have trouble understanding this, consult anybody who writes programs.</p> |
| 49 | <h3>Links to this page</h3> |
| 50 | <ul> |
| 51 | <li><a href="Scripting.html">Scripting</a> |
| 52 | <li><a href="Scripting_3__Simple_language_elements.html">Scripting 3. Simple language elements</a> |
| 53 | <li><a href="Scripting_3_4__String_variables.html">Scripting 3.4. String variables</a> |
| 54 | <li><a href="Scripting_5_1__Variables.html">Scripting 5.1. Variables</a> |
| 55 | <li><a href="Scripting_5_7__Vectors_and_matrices.html">Scripting 5.7. Vectors and matrices</a> |
| 56 | </ul> |
| 57 | <hr> |
| 58 | <address> |
| 59 | <p>© ppgb 20230201</p> |
| 60 | </address> |
| 61 | </body> |
| 62 | </html> |