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.) ![]() 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: 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: 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:
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.
| |||||||||||
|
Installation | Troubleshooting | Programming | Archives | Links Page last modified: 22nd August 2001 |