Modifications to \doc\bmxlang\arrays.html:
If someone can solve the issue of mult-dimensional auto arrays, please feel free to add it.
<html> <head> <title></title> <link rel="styleSheet" href=bmxlang.css type="text/css"> </head> <body> <h1>Arrays</h1> Arrays are used to store sequences of variables, or 'elements'. An element within an array is accessed by 'indexing' the array with an integer offset.<br> <br> The general syntax for indexing an array is: <div class=syntax> <font class=syntax>Array</font><font class=token>[</font><font class=syntax>Index1,Index2 etc...</font><font class=token>]</font> </div> <h2>Creating Arrays</h2> The most common way to create an array is when declaring a variable:<br> <pre class=demo> Local int_array[10] </pre> This will initialize the <font class=ident>int_array</font> variable with a 10 element array. You can declare an 'empty' array by using <font class=token>[]</font>: <pre class=demo> Local int_array[] </pre> An empty array is identical to an array with 0 elements.<br><br> Something to remember about arrays is that they are zero based, that is, the first element of the array has an index of 0. So if you declare an array with 4 elements, they will be indexed from 0 to 3. <pre class="demo"> Local int_array[4] 'Create an array with 4 elements (the default type for the array will be <a href="datatypes.html">Int</a>) int_array[0] = 1 'put the number 1 in the first element of the array. int_array[1] = 2 'put the number 2 in the second element of the array. int_array[2] = 3 'put the number 3 in the third element of the array. int_array[3] = 4 'put the number 4 in the fourth element of the array. print int_array[0] 'print the first element print int_array[3] 'print the fourth element </pre> Arrays may also be created 'on the fly' using the syntax: <div class=syntax> <font class=token>New</font> <font class=syntax>ElementType</font><font class=token>[</font><font class=syntax>Dimension1,Dimension2 etc...</font><font class=token>]</font> </div> This returns an array of the specified dimension(s) with each element initialized to <font class=token>Null</font>. For example:<br> <pre class=demo> Local int_array:Int[] int_array=New Int[10] </pre> 'Auto arrays' may be created using the syntax (an Auto Array is simply an array with predefined values in each index)<br>: <div class=syntax> <font class=token>[</font><font class=syntax>Element1,Element2 etc...</font><font class=token>]</font> </div> This returns a 1 dimensional array containing the specified elements, for example:<br> <pre class=demo> Local int_array[]=[1,2,3,4,5] </pre> Each element of an auto array must have exactly the same type. If necessary, you can use type conversions to enforce this.<br><br> <br> Arrays provide a <font class=token>Sort</font> method and a read only <font class=token>length</font> field. <font class=token>Sort</font> takes an optional <b>ascending</b> parameter that defaults to true. Here is an example of sorting an array: <pre class=code>Strict Local arr:String[]=["some","random","strings","in","a","string","array"] arr.Sort 'sort ascending Print "Array in ascending order..." For Local t:String=EachIn arr Print t Next arr.Sort False 'sort descending Print "Array in descending order..." For Local t:String=EachIn arr Print t Next</pre> To copy or resize an array, use a <a href=slices.html>slice</a>. <br><br> Example of resizing an array using slices: <pre class="code"> Local myArray[10] 'create an array with 10 elements (0..9) myArray = myArray[..20] 'increase number of elements in the array to 20 </pre> <h2>Multi-Dimensional Arrays</h2> To create an array with more than dimension, you can specify indexes seperated by commas: <pre class="code"> Local int_array1 : Int[4,4] 'create a 4x4 array Local int_array2 : Int[3,3,3] 'create a 3x3x3 array </pre> You can then refer to them like so: <pre class="code"> int_array1[0,0] = 100 int_array2[2,1,0] = 1000 </pre> <br> <h3>Multi-Dimensional Auto Arrays</h3> This is a slightly more advanced topic and as such, beginners may at first find it confusing.<br><br> It is not currently possible to create normal Mult-Dimensional Arrays AND auto-initialize them. You can however cheat a little: <pre class="code"> Local string_array:String[][] = [["Bob","Kate","Percy"],["Cecil","Edmund","Elizabeth"]] Print string_array[0][2] 'this will print Percy Print string_array[1][0] 'this will print Cecil </pre> What the above actually does is create an <font class=token>Array</font> of type <font class=token>Int Array</font>.<br> If you were to attempt to refer to it in the conventional manner it would cause an error (Incorrect number of array dimensions). <pre class="code"> Local string_array:String[][] = [["Bob","Kate","Percy"],["Cecil","Edmund","Elizabeth"]] Print string_array[0,0] 'this line would cause an error </pre> </body> </html>
If someone can solve the issue of mult-dimensional auto arrays, please feel free to add it.