Debian Codegraph @

docs/manual/Scripting_3_6___For__loops.html

raw
1 <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
2 <title>Scripting 3.6. “For” loops</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.6. “For” loops
13 </b></font></table></table>
14 <p>The power of a procedural programming language is most easily illustrated with the <i>for-loop</i>.</p>
15 <p>Take the example of the <a href="Scripting_3_5__String_queries.html">previous page</a>, whereas you wanted to know the text in the third interval of the first tier of a selected TextGrid. It&#8217;s easy to imagine that you actually want the texts of <i>all the first five</i> intervals. With knowledge from the previous sections, you could write it like this:</p>
16 <code> writeInfoLine: “The texts in the first five intervals:”<br></code>
17 <code> text$ = Get label of interval: 1, 1<br></code>
18 <code> appendInfoLine: “Interval 1: ”, text$<br></code>
19 <code> text$ = Get label of interval: 1, 2<br></code>
20 <code> appendInfoLine: “Interval 2: ”, text$<br></code>
21 <code> text$ = Get label of interval: 1, 3<br></code>
22 <code> appendInfoLine: “Interval 3: ”, text$<br></code>
23 <code> text$ = Get label of interval: 1, 4<br></code>
24 <code> appendInfoLine: “Interval 4: ”, text$<br></code>
25 <code> text$ = Get label of interval: 1, 5<br></code>
26 <code> appendInfoLine: “Interval 5: ”, text$<br></code>
27 <p>The result will be something like</p>
28 <p align=middle><img height=300 width=600 src=Scripting_3_6___For__loops_1.png></p><p>This can be done more nicely. The first step is to realize that the sentences starting with <code><font size=+1>text$</font></code> are similar to each other, and the sentence starting with <code><font size=+1>appendInfoLine</font></code> are also similar to each other. They only differ in the interval number, and can therefore be made <i>identical</i> by using a variable for the interval number, like this:</p>
29 <code> writeInfoLine: “The texts in the first five intervals:”<br></code>
30 <code> intervalNumber = 1<br></code>
31 <code> text$ = Get label of interval: 1, intervalNumber<br></code>
32 <code> appendInfoLine: “Interval ”, intervalNumber, “: ”, text$<br></code>
33 <code> intervalNumber = 2<br></code>
34 <code> text$ = Get label of interval: 1, intervalNumber<br></code>
35 <code> appendInfoLine: “Interval ”, intervalNumber, “: ”, text$<br></code>
36 <code> intervalNumber = 3<br></code>
37 <code> text$ = Get label of interval: 1, intervalNumber<br></code>
38 <code> appendInfoLine: “Interval ”, intervalNumber, “: ”, text$<br></code>
39 <code> intervalNumber = 4<br></code>
40 <code> text$ = Get label of interval: 1, intervalNumber<br></code>
41 <code> appendInfoLine: “Interval ”, intervalNumber, “: ”, text$<br></code>
42 <code> intervalNumber = 5<br></code>
43 <code> text$ = Get label of interval: 1, intervalNumber<br></code>
44 <code> appendInfoLine: “Interval ”, intervalNumber, “: ”, text$<br></code>
45 <p>A new trick that you see here is that as a numeric argument (<b>Interval number</b>, the second argument to <b>Get label of interval...</b>), you can use not only a number (as in all previous examples), but also a variable (<i><code><font size=+1>intervalNumber</font></code></i>). The rest of the script should be known stuff by now.</p>
46 <p>The script above is long, but it can be made much shorter with the use of a <i>for-loop</i>:</p>
47 <code> writeInfoLine: “The texts in the first five intervals:”<br></code>
48 <code> for intervalNumber from 1 to 5<br></code>
49 <code> text$ = Get label of interval: 1, intervalNumber<br></code>
50 <code> appendInfoLine: “Interval ”, intervalNumber, “: ”, text$<br></code>
51 <code> endfor<br></code>
52 <p>The two lines that were repeated five times in the previous version now show up with indentation between a <code><font size=+1>for</font></code> line and its corresponding <code><font size=+1>endfor</font></code>. Those two lines (the <code><font size=+1>text$</font></code> and the <code><font size=+1>appendInfoLine</font></code> line) are executed five times: for <i><code><font size=+1>intervalNumber</font></code></i> equal to 1, for <i><code><font size=+1>intervalNumber</font></code></i> equal to 2, for <i><code><font size=+1>intervalNumber</font></code></i> equal to 3, for <i><code><font size=+1>intervalNumber</font></code></i> equal to 4, and for <i><code><font size=+1>intervalNumber</font></code></i> equal to 5, in that order.</p>
53 <p>In the above example, using a loop does not do much more than save eight lines, at the cost of adding two new lines. But imagine the case in which you want to list <i>all</i> the texts in the intervals: the version without the loop is no longer possible. By contrast, the version <i>with</i> the loop is still possible, because we have the command <b>Get number of intervals...</b>, which gives us the number of intervals in the specified tier (here, tier 1). So you do:</p>
54 <code> numberOfIntervals = Get number of intervals: 1<br></code>
55 <code> writeInfoLine: “The texts in all ”, numberOfIntervals, “ intervals:”<br></code>
56 <code> for intervalNumber from 1 to numberOfIntervals<br></code>
57 <code> text$ = Get label of interval: 1, intervalNumber<br></code>
58 <code> appendInfoLine: “Interval ”, intervalNumber, “: ”, text$<br></code>
59 <code> endfor<br></code>
60 <p>This may yield something like</p>
61 <p align=middle><img height=300 width=600 src=Scripting_3_6___For__loops_2.png></p><p>This is the first script in this tutorial that is useful in itself. On the basis of it you can create all kinds of ways to list the texts in intervals. Here is how you would also list the durations of those intervals:</p>
62 <code> numberOfIntervals = Get number of intervals: 1<br></code>
63 <code> writeInfoLine: “The durations and texts in all ”, numberOfIntervals, “ intervals:”<br></code>
64 <code> for intervalNumber from 1 to numberOfIntervals<br></code>
65 <code> startTime = Get start point: 1, intervalNumber<br></code>
66 <code> endTime = Get end point: 1, intervalNumber<br></code>
67 <code> duration = endTime - startTime<br></code>
68 <code> text$ = Get label of interval: 1, intervalNumber<br></code>
69 <code> appendInfoLine: “Interval ”, intervalNumber, “ is ”, duration, “ seconds long and contains the text: ”, text$<br></code>
70 <code> endfor<br></code>
71 <h3>Links to this page</h3>
72 <ul>
73 <li><a href="Scripting.html">Scripting</a>
74 <li><a href="Scripting_3__Simple_language_elements.html">Scripting 3. Simple language elements</a>
75 <li><a href="Scripting_3_7__Layout.html">Scripting 3.7. Layout</a>
76 </ul>
77 <hr>
78 <address>
79 <p>© ppgb 20140111</p>
80 </address>
81 </body>
82 </html>