Array: Difference between revisions
mNo edit summary |
m (Better formatting) |
||
Line 6: | Line 6: | ||
==Declaring Arrays== | ==Declaring Arrays== | ||
Arrays are declared like this:<br> | Arrays are declared like this:<br> | ||
_array = [elementOne, elementTwo, ..., lastElement] | _array = [elementOne, elementTwo, ..., lastElement] | ||
Each element is either a literal of some type, or an expression of some type. | Each element is either a literal of some type, or an expression of some type. | ||
For example:<br> | For example:<br> | ||
_array = [1, "Word", (1 + [[ | _array = [1, "Word", (1 + [[damage]] [[player]])] | ||
The first two elements are literals (a [[Number]] and a [[String]]), whilst the third is an expression (a [[Number]]). | The first two elements are literals (a [[Number]] and a [[String]]), whilst the third is an expression (a [[Number]]). | ||
Line 19: | Line 17: | ||
The command for accessing elements is [[select_array|select]]. For example, suppose an array is <br> | The command for accessing elements is [[select_array|select]]. For example, suppose an array is <br> | ||
_array = [soldier1, soldier2, soldier3] | _array = [soldier1, soldier2, soldier3] | ||
Then, (_array [[select]] 0) is _soldier1, and (_array [[select]] 2) is soldier3. | Then, (_array [[select]] 0) is _soldier1, and (_array [[select]] 2) is soldier3. | ||
Line 34: | Line 31: | ||
-0.5 rounds up to 0<br> | -0.5 rounds up to 0<br> | ||
-0.5 <= index <= 0.5 rounds to 0<br> | -0.5 <= index <= 0.5 rounds to 0<br> | ||
0.5 rounds down to 0<br> | 0.5 rounds down to 0<br>7 | ||
0.5 < index < 1.5 rounds to 1<br> | 0.5 < index < 1.5 rounds to 1<br> | ||
1.5 rounds up to 2<br> | 1.5 rounds up to 2<br> | ||
Line 50: | Line 47: | ||
''Accesses which are out of bounds'':<br> | ''Accesses which are out of bounds'':<br> | ||
_array = []<br> | <pre>_array = []<br> | ||
_element = (_array select 0) | _element = (_array select 0) | ||
Line 57: | Line 54: | ||
_array = ["element"]<br> | _array = ["element"]<br> | ||
_element = (_array select -1) | _element = (_array select -1)</pre> | ||
''Accesses which are in bounds'':<br> | ''Accesses which are in bounds'':<br> | ||
<pre>_array = ["element"]<br> | |||
_array = ["element"]<br> | |||
_element = (_array select 0) | _element = (_array select 0) | ||
Line 68: | Line 63: | ||
_array = ["element"]<br> | _array = ["element"]<br> | ||
_element = (_array select -0.3) | _element = (_array select -0.3)</pre> | ||
==Changing an array== | ==Changing an array== | ||
Line 79: | Line 74: | ||
Analogy using [[Object|Objects]]:<br> | Analogy using [[Object|Objects]]:<br> | ||
_unit1 = player<br> | _unit1 = player<br> | ||
_unit2 = player | _unit2 = player | ||
The commands (player [[setDamage]] 0.5), (_unit1 [[setDamage]] 0.5) and (_unit2 [[setDamage]] 0.5) all have the same effect, since they all refer to the same object (the [[player]]). | |||
The commands (player [[ | |||
It is a similar case with arrays:<br> | It is a similar case with arrays:<br> | ||
_array = [1,2,3]<br> | _array = [1,2,3]<br> | ||
_array1 = _array<br> | _array1 = _array<br> | ||
_array2 = _array | _array2 = _array | ||
The commands (_array [[set]] [0, 7]), (_array1 [[set]] [0, 7]) and (_array2 [[set]] [0, 7]) all have the same effect, since they all refer to the same array. | The commands (_array [[set]] [0, 7]), (_array1 [[set]] [0, 7]) and (_array2 [[set]] [0, 7]) all have the same effect, since they all refer to the same array. | ||
Line 98: | Line 91: | ||
Example:<br> | Example:<br> | ||
_array = [1,2,3]<br> | _array = [1,2,3]<br> | ||
_array [[set]] [2, "Hello"] | _array [[set]] [2, "Hello"] | ||
Now, _array is [1, 2, "Hello"]. The 2nd index (which is the third element in the array, which was 3) gets replaced by "Hello". | Now, _array is [1, 2, "Hello"]. The 2nd index (which is the third element in the array, which was 3) gets replaced by "Hello". | ||
Line 107: | Line 100: | ||
Example:<br> | Example:<br> | ||
_array = [1]<br> | _array = [1]<br> | ||
_array [[set]] [3, 4] | _array [[set]] [3, 4] | ||
Now _array is [1, <Null>, <Null>, 4] | Now _array is [1, <Null>, <Null>, 4] | ||
Line 121: | Line 113: | ||
Example:<br> | Example:<br> | ||
_array = [1,2,3]<br> | _array = [1,2,3]<br> | ||
_array2 = +_array1 | _array2 = +_array1 | ||
Now _array and _array2 point to 2 different arrays, both of which have the contents [1,2,3]. | Now _array and _array2 point to 2 different arrays, both of which have the contents [1,2,3]. | ||
Line 131: | Line 122: | ||
Example:<br> | Example:<br> | ||
_array1 = [player, | _array1 = [player, , "String"]<br> | ||
_array2 = [player, 2]<br> | _array2 = [player, 2]<br> | ||
_array3 = _array1 + _array2 | _array3 = _array1 + _array2 | ||
After this _array3 refers to a new array which has the contents [player, 7, "String", player, 2] | After this _array3 refers to a new array which has the contents [player, 7, "String", player, 2] | ||
Line 142: | Line 132: | ||
Example:<br> | Example:<br> | ||
_array1 = [1,2,player,2,"String","String",3]<br> | _array1 = [1,2,player,2,"String","String",3]<br> | ||
_array2 = [2,player,"String"]<br> | _array2 = [2,player,"String"]<br> | ||
_array3 = _array1 - _array2 | _array3 = _array1 - _array2 | ||
The result is that _array3 is a new array which has the contents [1, 3]. | The result is that _array3 is a new array which has the contents [1, 3]. |
Revision as of 08:25, 15 September 2006
Description
An array is a list of items. Each item may be any of the variable types.
The items in an array are referred to as its elements.
Arrays have an order. That is, any element in the array comes either before, or after, any other element in the array.
Declaring Arrays
Arrays are declared like this:
_array = [elementOne, elementTwo, ..., lastElement]
Each element is either a literal of some type, or an expression of some type.
For example:
_array = [1, "Word", (1 + damage player)]
The first two elements are literals (a Number and a String), whilst the third is an expression (a Number).
Accessing elements
Every element in the array has an index. The index says what position the element is in the array. Indices are how elements are accessed. Indices begin at zero, and continue up to (size of array - 1).
The command for accessing elements is select. For example, suppose an array is
_array = [soldier1, soldier2, soldier3]
Then, (_array select 0) is _soldier1, and (_array select 2) is soldier3.
Looping to access elements
In order to loop (or iterate) through every element in an array, for any array, we have to know the array's size.
The command for finding an array's size is count.
Index rounding
In OFP script, indices are rounded to the nearest whole number. A bounday case (X.5, where X is any whole number) rounds to the nearest even whole number
Boundary cases:
-0.5 rounds up to 0
-0.5 <= index <= 0.5 rounds to 0
0.5 rounds down to 0
7
0.5 < index < 1.5 rounds to 1
1.5 rounds up to 2
1.5 <= index <= 2.5 rounds to 2
2.5 rounds down to 2
2.5 < index < 3.5 rounds to 3
3.5 rounds up to 4
Other indices follow this pattern.
When an index is out of bounds
If a rounded index refers to a position in an array that is invalid:
- If the index is negative, an Error Zero Divisor error message will be displayed.
- If the index is positive, the returned value will be of the null type.
Accesses which are out of bounds:
_array = []<br> _element = (_array select 0) _array = ["element"]<br> _element = (_array select 1) _array = ["element"]<br> _element = (_array select -1)
Accesses which are in bounds:
_array = ["element"]<br> _element = (_array select 0) _array = ["element"]<br> _element = (_array select 0.1) _array = ["element"]<br> _element = (_array select -0.3)
Changing an array
There are two types of operations to change an array: operations which change the underlying array, and operations which return a new array, but leave the old array intact.
Array references
Array variables behave the same as Object references, and differently to String, Number and other variable types. An array variable holds a pointer to an array, or in other words, the location of an array. Any number of different variables can refer to the same array.
Analogy using Objects:
_unit1 = player
_unit2 = player
The commands (player setDamage 0.5), (_unit1 setDamage 0.5) and (_unit2 setDamage 0.5) all have the same effect, since they all refer to the same object (the player).
It is a similar case with arrays:
_array = [1,2,3]
_array1 = _array
_array2 = _array
The commands (_array set [0, 7]), (_array1 set [0, 7]) and (_array2 set [0, 7]) all have the same effect, since they all refer to the same array.
Setting elements
Individual elements in an array can be set to different values.
This is done via the set operator.
The previous element at the specified index gets replaced with the new one.
Example:
_array = [1,2,3]
_array set [2, "Hello"]
Now, _array is [1, 2, "Hello"]. The 2nd index (which is the third element in the array, which was 3) gets replaced by "Hello".
If the index given by the set operator is out of bounds,
- If the index rounded to a negative number, then an Error Zero Divisor message will be displayed in game.
- If the index rounded to a positive number, then the array will resize to incorporate the index as its last value. Each element between the last valid element, and the new set element, will be the null type
Example:
_array = [1]
_array set [3, 4]
Now _array is [1, <Null>, <Null>, 4]
Array copying, addition and substraction
Each of these commands returns a new array, and leaves the old array or arrays unchanged.
Copying
This is done by the + unary operator. It copies an array, and sets the array variable to point to this new array.
Example:
_array = [1,2,3]
_array2 = +_array1
Now _array and _array2 point to 2 different arrays, both of which have the contents [1,2,3].
Addition
This is done by the + binary operator. It takes two arrays, and returns a new array which containes all of the first array, followed by all of the second array.
Example:
_array1 = [player, , "String"]
_array2 = [player, 2]
_array3 = _array1 + _array2
After this _array3 refers to a new array which has the contents [player, 7, "String", player, 2]
Subtraction
This is done by the - binary operator. It takes 2 arrays, and returns a new array that contains all of the items in the first array that were not in the second array.
Example:
_array1 = [1,2,player,2,"String","String",3]
_array2 = [2,player,"String"]
_array3 = _array1 - _array2
The result is that _array3 is a new array which has the contents [1, 3].
Note:
Doing <some array> - <some array> will always return an empty array [].