Working with arrays
 

As mentioned in the introduction, arrays are a linear block of memory which can hold a certain number of variables of a specific type. Both the size of the array and the type of variable each item holds are specified when you create the array. Due to the array being a linear block of memory, you access items in the array by using an 'index' - a number which specifies the item you want to access. Each item in the array is treated like a single variable of the specific type, but because they are arranged in an array, they are easier to perform processing over multiple items. (This is shown below, where each box represents a single item of data storage.)
Visual representation of variables and arrays

To create an array, you use the Dim command - dim stands for dimension. You then give the array a name (same as using a variable name [give it a meaningful and consistent name]), a type and the maximum size of the array. This is shown below:

Code Dim foo.w(20)
This creates an array called foo. Each item in the foo array is a word type, and the size of the array is 20 items (or the value of whatever expression you put in those brackets). Since the size is 20 in this example, we can access the items from index 0 to index 20 (so we really have 21 items in the array).

An item in an array is accessed by using the array name followed by an index value in brackets - this means that you can have fast and direct access to any item in the array, but array items are more awkward to move around. Since each item in the array works in exactly the same way as a normal variable, we can use all the same expressions from the previous topic about variables (and the same rules about types and expressions apply to variables), as shown in this small example:

Code Dim foo.w(10) ; Create the array called foo
foo(0)=23
foo(1)=45
foo(2)=foo(0)+foo(1)
NPrint foo(2)
foo(3)=1
NPrint foo( foo(3) ) ; Any expression can be used as the index
MouseWait
End
The last line there shows you that any expression can be used as the index (the value in brackets). In the above example, the value of foo(3) is used to access and print another value from the foo array (in this case it will print the value of foo(1), since the value of foo(3) is 1 and is being used as the index).

A couple of important points to note about arrays:

  1. Every array must have a maximum index (i.e. the value you put in the brackets when using the Dim command) less than 65535. This is because of the way Blitz accesses arrays when using Newtypes (Newtypes are explained in the next topic). It can work for the built-in variable types to use larger values, but the debugger gives you an error - this is actually an error in the debugging code, your program will work OK but only with the built-in types.
  2. You can re-create arrays with different sizes by using the Dim command again for the array and using a different expression in the brackets. You will lose the current contents of the array however.
    Code Dim foo.w(10) ; Create the array called foo with a maximum index of 10
    Dim foo.w(20) ; foo now has a maximum item number of 20, but the contents of the array have been erased
  3. You can only access values in an array using index values from 0 to the value you used in the Dim command. If you use values outside this range, you will be accessing memory which is not part of the array, so do not do it! (You will get an error from the debugger if it is enabled, but if it is not, you run the risk of corrupting memory and crashing the computer!)
    Code Dim foo.w(10) ; Create the array called foo
    foo(-1)=1 ; This is NOT correct
    foo(0)=1 ; This is correct
    foo(10)=1 ; This is correct
    foo(11)=1 ; This is NOT correct

Using arrays will probably become much more clear when we reach the topic about Program Flow, when some examples of processing entire arrays will be given.


Previous: Using arrays and lists Programming contents Next: Working with lists


News | Introduction | Search | List/Forum/IRC Webring | Contact
Installation | Troubleshooting | Programming | Archives | Links
Return to Programming main
Page last modified: 22nd August 2001