| 1 | <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 | <title>Scripting 5.6. Arrays and dictionaries</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 5.6. Arrays and dictionaries |
| 13 | </b></font></table></table> |
| 14 | <p>You can use arrays of numeric and string variables:</p> |
| 15 | <code> <b>for</b> i <b>from</b> 1 <b>to</b> 5<br></code> |
| 16 | <code> square [i] = i * i<br></code> |
| 17 | <code> text$ [i] = <a href="_mid-S_.html">mid$</a> (“hello”, i)<br></code> |
| 18 | <code> <b>endfor</b><br></code> |
| 19 | <p>After this, the variables <code><font size=+1>square[1]</font></code>, <code><font size=+1>square[2]</font></code>, <code><font size=+1>square[3]</font></code>, <code><font size=+1>square[4]</font></code>, <code><font size=+1>square[5]</font></code>, <code><font size=+1>text$[1]</font></code>, <code><font size=+1>text$[2]</font></code>, <code><font size=+1>text$[3]</font></code>, <code><font size=+1>text$[4]</font></code>, and <code><font size=+1>text$[5]</font></code> contain the values 1, 4, 9, 16, 25, “h”, “e”, “l”, “l”, and “o”, respectively:</p> |
| 20 | <code> <a href="_writeInfoLine_.html">writeInfoLine</a>: “Some squares:”<br></code> |
| 21 | <code> <b>for</b> i <b>from</b> 1 <b>to</b> 5<br></code> |
| 22 | <code> <a href="_appendInfoLine_.html">appendInfoLine</a>: “The square of ”, i, “ is ”, square [i]<br></code> |
| 23 | <code> <b>endfor</b><br></code> |
| 24 | <code style="color:red">=></code><br> |
| 25 | <code> Some squares:<br></code> |
| 26 | <code> The square of 1 is 1<br></code> |
| 27 | <code> The square of 2 is 4<br></code> |
| 28 | <code> The square of 3 is 9<br></code> |
| 29 | <code> The square of 4 is 16<br></code> |
| 30 | <code> The square of 5 is 25<br></code> |
| 31 | <p>In the examples above, the <i>index</i> into the array was always a number. A <i>hash</i> or <i>dictionary</i> is an array variable where the index is a string:</p> |
| 32 | <code> age [“John”] = 36<br></code> |
| 33 | <code> age [“Babs”] = 39<br></code> |
| 34 | <code> <a href="_writeInfoLine_.html">writeInfoLine</a>: “John is ”, age [“John”], “ years old.”<br></code> |
| 35 | <code style="color:red">=></code><br> |
| 36 | <code> John is 36 years old.<br></code> |
| 37 | <h2>See also</h2> |
| 38 | <p>You can use any number of array and dictionary variables in a script, but for many applications, namely whenever it were useful to look at a numeric array as a single object, it may be better to use vectors and matrices (see <a href="Scripting_5_7__Vectors_and_matrices.html">Scripting 5.7. Vectors and matrices</a>) or to use Matrix or Sound objects.</p> |
| 39 | <h3>Links to this page</h3> |
| 40 | <ul> |
| 41 | <li><a href="Scripting.html">Scripting</a> |
| 42 | <li><a href="Scripting_5__Language_elements_reference.html">Scripting 5. Language elements reference</a> |
| 43 | <li><a href="What_was_new_in_5_3_.html">What was new in 5.3?</a> |
| 44 | </ul> |
| 45 | <hr> |
| 46 | <address> |
| 47 | <p>© Paul Boersma 2017-07-18,2025</p> |
| 48 | </address> |
| 49 | </body> |
| 50 | </html> |