# Arrays

### From Original War Support Wiki

Tutorials -> Arrays

## Contents |

## Introduction

An array, or a list, is a collection of values (of any type). Each value is separated by comma. A list starts with **[** and ends with **]**.

The position of specific value in an array is called its **index number** (or **index** for short). E.g. in this list: [5, 7, 4, 2], the value "7" has index number "2".

## Examples of working with arrays

**1)** To use only one value from an array:

list:= [2, 3, 4]; one_element:= list [3]; // one_element will be equal to 4 because third [3] value in list is 4

**2)** To joint some arrays into one:

Array1:= [2, 3, 4]; Array2:= [3, 4, 5]; JointArray:= Array1 ^ Array2;

**JointArray** will contain: [2, 3, 4, 3, 4, 5]

However sometimes we need different result as when working with units we don't need more than one of each in a list. That's where **union** operator comes in handy:

JointArray:= [John, Bobby, Joan] union [John, Joan, Cyrus, Radzio];

**JointArray** will contain: [John, Bobby, Joan, Cyrus, Radzio]

**Union** excludes all repeated values in each list, then excludes in the right list these which are already in the left and finally joints two lists.

It can be used also for removing repeated values in a single list:

JointArray:= [1, 2, 1, 1, 2, 3] union [];

**JointArray** will contain: [1, 2, 3]

**3)** To exclude a value or a whole array from another array:

MyArray:= [1, 2, 3, 4, 5] diff [1, 4]; MyArray:= MyArray diff 5;

**MyArray** will contain: [2, 3]

Note: **diff** removes repeated values (like **union** - see above) and can be used like **union** on a single list with the same result

**4)** To find common elements in two arrays. The "isect" operator retains all elements from the first array which are also present in the second array.

MyArray := [1, 2, 3, 4] isect [3, 4, 5];

The result is [3, 4]. For the methametically inclined this corresponds to the *intersection* operator on sets. Like **union** and **diff** **isect** removes repeated values.

**5)** To work on each value in an array:

var a, MyArray; //some code MyArray:= [John, Joan, Bobby]; for a in list do begin if (not IsDead(a)) then KillUnit(a); end;

And everybody alive in the list will get killed.

## Arrays, maths, comparisons

When we use an array in mathematical expression like this:

[1, 3] * [9, 3, 7] + [11]

it's treated as integer with a value being its size; that is how many values it contains. So the result from the above expression is 2 * 3 + 1 = 7

The same happens if you compare list with an integer: [1, 5, 3, 4, 0] = 5 is true.

However, if you compare two arrays, they will be treated as equal only if they have the same size and same values at same indexes:

true: [1, 2, 3, 4] = [1, 2, 3, 4] false: [1, 2, 3, 4] = [1, 2, 3, 4, 4] [1, 2, 3, 4] = [2, 1, 4, 3]

## Size of the array

To know what is the size of the array, use this function :

Function size( _array ); begin Result := 0; for _x in _array do begin Result := Result + 1; end; end;