Learning Petit Lesson 4: Arrays

''This is intended to be the 4th lesson in my tutorial series, but I have posted before lessons 2 and 3 due to a request for an array lesson

This lesson covers arrays, which are used to store collections of information in list or table format. We will go over some basic definitions, the structure of arrays, and then how to use them.

General concept and definitions
In general terms, an array is a collection of variables that are referred to by a number instead of a name. Arrays are more or less identical in concept to mathematical matrices. If you need to store a large amount of information in a list or a table, arrays are the tool you use. Arrays have a property called dimension that is analagous to dimensions in a mathematical space or graphics. In Petit Computer, arrays can be one-dimensional (1D) or two-dimensional (2D). We'll go over this soon.

Again, an array is a collection of unnamed variables. Each variable in an array is called an element, and you access an element by its index, which is a number denoting its position in the array. Elements in an array must usually be of the same type. In Petit, you can make arrays of numbers and arrays of strings, but not an array that contains both numbers and strings.

Don't worry if this all sounds confusing, it's actually fairly simple.

1-Dimensional arrays
Think of a 1D array as a list. You use a 1D array just like a list, to store a collection of separate but related values. For example, you might want to store names for five different characters: "Luke", "Han", "Leia", "Chewie", "Lando". That's a list, so you can store it in a 1D array.

You declare a 1D array like this:

The DIM command creates an array with the name you provide, in this case NAMES$ (as with variables, a $ at the end of the name indicates the array contains strings). The number in parentheses is the size of the array. We just created an array of strings with space for 5 elements.

You store or load a value from an array like this:

After we create the array, we access elements in the array by using the array name followed by the index in parentheses. In the above example, Luke is at index 0 and Han is at index 1, etc. Again, the index refers to the position of a value in the array, and the first index is always 0. In this example, the array has a size of 5 with indexes 0 - 4. This is called 0-based indexing. It is very important to remember the first index is always 0 and the last index is 1 less than the length.

Be careful not to confuse arrays and functions, they can have exactly the same syntax (see Notes).

2-Dimensional arrays
Think of a 2D array as a spreadsheet with rows and columns. You should use them when you have several objects with the same properties; for example, if you had several weapons that had a damage, rate of fire, and range.

You declare and use a 2D array like this:
 * was accessing an array. Many programming languages use [] brackets for specifying array indexes so you don't confuse them with functions. I read that Petit's sequel SmartBASIC will use [] brackets for this purpose.
 * A 2D array is sometimes referred to as an "array-of-arrays" because it's basically an array of 1D arrays, but it's much easier to think of it as a spreadsheet