Difference between revisions of "Introduction to OBSE arrays"
m
WIKI formatting has been messed up. Fixed.
imported>QQuix m (→Declaring and initializing arrays: Formatting has been messed up. Fixed.) |
imported>QQuix m (WIKI formatting has been messed up. Fixed.) |
||
Line 168: | Line 168: | ||
1. Explicitly initializing it using ar_Construct | 1. Explicitly initializing it using ar_Construct | ||
::When initializing an array with ar_Construct, the type of array must be provided as the argument: | ::When initializing an array with ar_Construct, the type of array must be provided as the argument: | ||
:::let MyArray := ar_Construct array | :::let MyArray := ar_Construct array | ||
:::let MyArray := ar_Construct map | :::let MyArray := ar_Construct map | ||
Line 175: | Line 176: | ||
2. Assigning the value of another array_var to it | 2. Assigning the value of another array_var to it | ||
:: In this case MyArray will be of the same type of SomeArray (actually, both refer to the same array) | :: In this case MyArray will be of the same type of SomeArray (actually, both refer to the same array) | ||
:::let SomeArray := ar_Construct array | :::let SomeArray := ar_Construct array | ||
:::Let MyArray := SomeArray | :::Let MyArray := SomeArray | ||
Line 190: | Line 192: | ||
The keys, of course, must comply with the type of array: | The keys, of course, must comply with the type of array: | ||
::let MyArray := ar_Construct array | |||
::Let MyArray[0] := 123 | |||
::Let MyArray[1] := 456 | |||
::Let MyArray[2] := 789 | |||
::let MyArray := ar_Construct map | |||
::Let MyArray[-1.5] := 123 | |||
::Let MyArray[2.5] := 456 | |||
::Let MyArray[3] := 789 | |||
::let MyArray := ar_Construct stringmap | |||
::Let MyArray["Cost"] := 123 | |||
::Let MyArray["Qty"] := 456 | |||
::Let MyArray["Taxes"] := 789 | |||
Line 218: | Line 217: | ||
The [[Let]] statement is array-aware and is the most common way of accessing array elements | The [[Let]] statement is array-aware and is the most common way of accessing array elements | ||
::float MyFloat | |||
::. . . | |||
::Let MyFloat := MyArray["PosX"] | |||
::Player.setpos x MyFloat | |||
::ref MyRef | |||
::. . . | |||
::Let MyRef := MyArray["ReferenceID"] | |||
::MyRef.Kill | |||
::string_var MyString | |||
::. . . | |||
::Let MyString := MyArray[3.7] | |||
::MessageBoxEX "The element text is: %z" MyString | |||
In all examples up to here, keys were explicitly coded, but they may also be in a variable and the variable itself going within the brackets: | In all examples up to here, keys were explicitly coded, but they may also be in a variable and the variable itself going within the brackets: | ||
::string_var MyString | |||
::. . . | |||
::Let MyString := "Item value" | |||
::Let MyArray[MyString] := 300 | |||
::short MyShort | |||
::. . . | |||
::Let MyShort := 3 | |||
::Let MyArray[MyShort] := "Text" | |||
Line 267: | Line 264: | ||
ForEach loops iterate over the elements of an array. | ForEach loops iterate over the elements of an array. | ||
::array_var item | |||
::short MyShort | |||
::string_var MyString | |||
::. . . | |||
::ForEach item <- MyArray | |||
:::Let MyShort := item["key"] | |||
:::Let MyString := item["value"] | |||
:::MessageEX "The element %g is: %z" MyShort MyString | |||
::Loop | |||
At each iteration, "item" is initialized with two elements: | At each iteration, "item" is initialized with two elements: | ||
Line 294: | Line 291: | ||
The same example as above using a While loop: | The same example as above using a While loop: | ||
::short MyIndex | |||
::string_var MyString | |||
::. . . | |||
::Let MyIndex := 0 | |||
::While MyIndex < ar_size MyArray | |||
:::Let MyString := MyArray[MyIndex] | |||
:::MessageEX "The element %g is: %z" MyIndex MyString | |||
:::MyIndex += 1 | |||
::Loop | |||
Notice that with while loops, you have to increase the index yourself, as opposed to ForEach loops that automatically go to the next element. | Notice that with while loops, you have to increase the index yourself, as opposed to ForEach loops that automatically go to the next element. | ||
Line 310: | Line 307: | ||
A somewhat different (perhaps more elegant) way of increasing the index | A somewhat different (perhaps more elegant) way of increasing the index | ||
::short MyIndex | |||
::string_var MyString | |||
::. . . | |||
::Let MyIndex := -1 | |||
::While (MyIndex += 1) < ar_size MyArray | |||
:::Let MyString := MyArray[MyIndex] | |||
:::MessageEX "The element %g is: %z" MyIndex MyString | |||
::Loop | |||
While loops also allows for walking Arrays backward: | While loops also allows for walking Arrays backward: | ||
::short MyIndex | |||
::string_var MyString | |||
::. . . | |||
::Let MyIndex := ar_size MyArray | |||
::While (MyIndex -= 1) >= 0 | |||
:::Let MyString := MyArray[MyIndex] | |||
:::MessageEX "The element %g is: %z" MyIndex MyString | |||
::Loop | |||