An array is a variable that can store a series of numbers. You can imagine it like a table containing a row of values. Arrays have certain characteristics:

- The length of an array is the integer number of elements stored in the table.
- The elements of the table are numbered from 0 to len-1 (where len is the length of the array).
- The integer number corresponding to the position of one element in the table is called the index.

Scripts are very useful to manipulate arrays.

`Var array-name : array [N..M] of Type-of-values-stored-in -the-array;`

```
Var tab1 : array [0..127] of integer;
// this creates a table that stores integer values.
Var MIDIValuesII : array [1..16] of TMIDI;
// this creates a table that stores MIDI codes.
```

`Var array-name : array of Type-of-values-stored-in -the-array;`

```
Var tab1 : array of integer;
// this creates a table that stores integer values.
Var MIDIValues : array of TMIDI;
// this creates a table that stores MIDI codes.
```

Use the built-in procedure:

`SetArrayLength(Array-name, length); // where length is an integer.`

```
Var tab1 : array of integer;
SetArrayLength(tab1, 10); // declares an table containing 10 integers.
```

Use the built-in function:

`GetArrayLength(array-name); // the result is an integer.`

```
Var tab1 : array of integer;
Var x : integer;
SetArrayLength(tab1, 10);
X := GetArrayLength(tab1); // the value 10 is assigned to x.
```

Remember that the values in an array are indexed from 0 to the length of the array minus 1.

The first element's index is 0, the 4th one's index is 3 the last one's index is len-1
(`len`

is the length of the array).

You can access an array value by typing the array name and the index between brackets.

```
Tab1[0] // this is the first value stored in the table
Tab1[5] // this is the 6th value stored in the table
```

To store values into an array just do:

```
Var tab1 : array [1..4] of integer;
Tab1[0] :=1;
Tab1[1] :=4;
Tab1[2] :=3;
Tab1[3] :=12;
// this stores the values (1,4,3,12) into the table.
```

You can also store values this way:

```
Var tab2 := array [1..4] of single;
Tab2 := [1.2 , 2.23 , 4 , 5.1];
// this stores the values 1.2 , 2.23 , 4 , 5.1 into the array.
// Notice the commas to separate values
```

To read a value from an array just do:

```
Var x : integer;
X :=tab1[2];
// this stores to x the value of the third element of the array.
```

Of course, the index between brackets can be a variable, for instance:

`Tab1[i];`

Sometimes it's convenient to go through the whole array to read or store values.
To do this, you can use the `for loop`

.

Let's say we want to build a table of 50 integers containing numbers from 1 to 50. Doing this index by index would be time consuming.

You can simply do it this way:

```
Var tab1 : array[0..49] of integer;
Var i : integer;
For i :=0 to 49 do
Begin
Tab1[i] := i+1; // stores the value i+1 to the ith element
End;
```

Let`s say you have a MIDI input in your script, waiting for MIDI notes.

You don't know in advance how many notes you'll receive at a time (a single note, or a 4 notes chord).

The good strategy in this case would be to create an array storing
the incoming MIDI codes (let's call this array `MIDIInArray`

).

Then you measure the length (we'll see how to do this later) of this array
(the number of notes), let's call the length `NbOfMIDI`

.

Then you have to process each MIDI note in the following loop:

```
For i:=0 to NbOfMIDI-1 do
Begin
// here you do the processing on MIDIInArray[i] (the ith note)
End;
```

Until now, we've only used arrays containing one row of numbers. They are said to have one dimension.

In some situations, you'll need to create a table with rows and columns.

This is actually an array (row) of arrays (columns). It has two dimensions. You can create arrays of any dimension (array of array of array, etc. if you like, but you'll mostly use 1 or 2 dimensions arrays.

To create a bi-dimensional array, you first have to create a custom type for your 'columns', then create an array of 'columns'.

Imagine you want to store all the modes of the major scale into an array
called `MajScaleModes`

. A mode will be stored in an array of integers.
We'll create a new Type for these arrays called `tMode`

. These integers will
represent the intervals in half-tones from the tonic.

Each mode has 7 notes, there are 7 modes so the table must be 7x7.

Let's create a two dimensions array of integer:

```
Type tMode : array [1..7] of integer;
Var MajScaleModes : array of tMode;
Var ionian, dorian, prhygian, lydian, myxolydian, aeolian, locrian : tMode;
MajScaleModes := [ionian, dorian, prhygian, lydian, myxolydian, aeolian, locrian];
Ionian := [0, 2, 4, 5, 7, 9, 11];
Dorian := /// etc.
```

If we want to assign a major third (4 half-tones) to the 3rd value of ionian we can do:

`Ionian[2] := 4;`

Or directly:

```
MajScaleModes[0,2] := 4;
// You can read this assign the value 4 to the 3rd element of the 1st
element (ionian) of MajScaleModes.
```

This is the value of the fifth (index =4) of the myxolydian mode (index = 5):

`MajScaleModes[5,4]`

A common error when scripting is the type mismatch.

This happens when you try to store a value of a certain type into a variable of another type.

```
Var x : integer;
x := GetValue(inA);
```

would return an error, since GetValue(inA) is by definition a single, and x is declared as an integer (which is not stored in memory with the same structure). There is so to speak not enough room in x to store the single value.

You have to be careful about the size of the types of variables: For numerical values, these are the types you can use from the largest to the smallest:

` extended, double, single, longint, int, byte`

In general a larger type is compatible with a smaller one, but the opposite doesn't work:

```
Var x : single;
Var y : double;
Y := x; // this should work
X := y; // this will return a Type mismatch error
```

Fortunately there are built-in functions to avoid these problems:
**the type conversion functions.**

```
Round(val) ; // this converts extended, double or single to integer.
IntToString(i: longint): string; // this converts a number into a string of characters
// (that means that inToString(1423)is no more the
// number 1423, but a string containing the characters
StrtoInt(s: string): longint ; // does the opposite
FloatToStr(e: extended): string; and StrToFloat(s: string):
extended; // those are the same as before, but for floating point numbers.
```

You can also built your own type conversion function.

Let's build a function to convert an integer to a boolean.
If the **integer** is equal or larger than 1, the **boolean** is **TRUE**,
if the **integer** is 0 or less the **boolean** is **FALSE**:

```
Function IntToBool(i: integer): boolean;
Begin
If i>=1 then result := True
else result := False;
End;
```

Now we can call our function:

```
Var x : integer;
Var y : boolean;
x := 9;
y := IntToBool(x); // this will set y to TRUE
```

**NB.** We could also have written our function with a more compact code:

```
Function IntToBool(i: integer): boolean;
Begin
result := (i>=1);
End;
```

Switches, and buttons can by definition only have values of 0 or 1, these
0 and 1 are considered as **single** when you access the switch or button
with `GetValue`

.

**version** 4.1.200407