Difference between revisions of "Introduction to OBSE arrays"

278 bytes removed ,  07:06, 29 December 2013
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:


<dl><dd><dl><dd>
let MyArray := ar_Construct array
Let MyArray[0] := 123
Let MyArray[1] := 456
Let MyArray[2] := 789
</dl></dl>


<dl><dd><dl><dd>
::let MyArray := ar_Construct array
let MyArray := ar_Construct map
::Let MyArray[0] := 123
Let MyArray[-1.5] := 123
::Let MyArray[1] := 456
Let MyArray[2.5] := 456
::Let MyArray[2] := 789
Let MyArray[3] := 789
 
</dl></dl>
 
::let MyArray := ar_Construct map
<dl><dd><dl><dd>
::Let MyArray[-1.5] := 123
let MyArray := ar_Construct stringmap
::Let MyArray[2.5] := 456
Let MyArray["Cost"] := 123
::Let MyArray[3] := 789
Let MyArray["Qty"] := 456
 
Let MyArray["Taxes"] := 789
 
</dl></dl>
::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


<dl><dd><dl><dd>
 
float MyFloat
::float MyFloat
. . .
::. . .
Let MyFloat := MyArray["PosX"]  
::Let MyFloat := MyArray["PosX"]  
Player.setpos x MyFloat
::Player.setpos x MyFloat
</dl></dl>
 
<dl><dd><dl><dd>
 
ref MyRef
::ref MyRef
. . .
::. . .
Let MyRef := MyArray["ReferenceID"]  
::Let MyRef := MyArray["ReferenceID"]  
MyRef.Kill
::MyRef.Kill
</dl></dl>
 
<dl><dd><dl><dd>
 
string_var MyString
::string_var MyString
. . .
::. . .
Let MyString := MyArray[3.7]
::Let MyString := MyArray[3.7]
MessageBoxEX "The element text is: %z" MyString
::MessageBoxEX "The element text is: %z" MyString
</dl></dl>




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:


<dl><dd><dl><dd>
 
string_var MyString
::string_var MyString
. . .
::. . .
Let MyString := "Item value"
::Let MyString := "Item value"
Let MyArray[MyString] := 300
::Let MyArray[MyString] := 300
</dl></dl>
 
<dl><dd><dl><dd>
 
short MyShort
::short MyShort
. . .
::. . .
Let MyShort := 3
::Let MyShort := 3
Let MyArray[MyShort] := "Text"
::Let MyArray[MyShort] := "Text"
</dl></dl>




Line 267: Line 264:
ForEach loops iterate over the elements of an array.
ForEach loops iterate over the elements of an array.


<dl><dd><dl><dd>
 
array_var item
::array_var item
short MyShort
::short MyShort
string_var MyString
::string_var MyString
. . .
::. . .
ForEach item <- MyArray
::ForEach item <- MyArray
    Let MyShort := item["key"]
:::Let MyShort := item["key"]
    Let MyString := item["value"]
:::Let MyString := item["value"]
    MessageEX "The element %g  is: %z" MyShort MyString
:::MessageEX "The element %g  is: %z" MyShort MyString
Loop
::Loop
</dl></dl>
 


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:
<dl><dd><dl><dd>
 
short MyIndex
::short MyIndex
string_var MyString
::string_var MyString
. . .
::. . .
Let MyIndex := 0
::Let MyIndex := 0
While MyIndex < ar_size MyArray
::While MyIndex < ar_size MyArray
    Let MyString := MyArray[MyIndex]
:::Let MyString := MyArray[MyIndex]
    MessageEX "The element %g  is: %z" MyIndex MyString
:::MessageEX "The element %g  is: %z" MyIndex MyString
    MyIndex += 1
:::MyIndex += 1
Loop
::Loop
</dl></dl>
 


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
<dl><dd><dl><dd>
 
short MyIndex
 
string_var MyString
::short MyIndex
. . .
::string_var MyString
Let MyIndex := -1
::. . .
While (MyIndex += 1) < ar_size MyArray
::Let MyIndex := -1
    Let MyString := MyArray[MyIndex]
::While (MyIndex += 1) < ar_size MyArray
    MessageEX "The element %g  is: %z" MyIndex MyString
:::Let MyString := MyArray[MyIndex]
Loop
:::MessageEX "The element %g  is: %z" MyIndex MyString
</dl></dl>
::Loop
 




While loops also allows for walking Arrays backward:
While loops also allows for walking Arrays backward:
<dl><dd><dl><dd>
 
short MyIndex
 
string_var MyString
::short MyIndex
. . .
::string_var MyString
Let MyIndex := ar_size MyArray
::. . .
While (MyIndex -= 1) >= 0
::Let MyIndex := ar_size MyArray
    Let MyString := MyArray[MyIndex]
::While (MyIndex -= 1) >= 0
    MessageEX "The element %g  is: %z" MyIndex MyString
:::Let MyString := MyArray[MyIndex]
Loop
:::MessageEX "The element %g  is: %z" MyIndex MyString
</dl></dl>
::Loop
 




Anonymous user