|
Lists and Arrays
Lists are a form to group values under one variable. Lists are also know as Arrays or Matrix. To access a particular value in a List we use an index or a key value.
In bOSL there are two types of Lists the traditional Array which we access by a numeric index and a Hash type which we access using the name of the value as the key.
ARRAY
To define an array we use a DIM statement. The DIM statement looks like this:
In the example above the array is named MyArray. Inside the brackets we indicate the amount or rows in our array or in other words we set the dimensions. The first row in the array is row zero (0) and the last row is row five (5). Our array has a total of 6 rows. The brackets are followed by the data type of our array. In this example the array will store six rows of strings.
To access a given row in code we use the name of the array followed by the brackets. Inside the brackets we indicate the index of the row we want to manipulate. If we attempt to access a row past the last row defined an "Out of bounds" error will be raised. The same will happen if we try to access a negative row (lower than 0).
MyArray[0] = "Jose"
MyArray[1] = "Joe"
MyArray[2] = "Lourdes"
msgbox(MyArray[2])
Sub Main()
dim s as string
msgbox("Array and Array of objects")
dim a(3) as string
a(0) = "Jose"
a(1) = "L"
a(2) = "Cuevas"
s = a(0) & " " & a(1) & " " & " " & a(2)
msgbox("Hello " & s)
dim d(3) as date
d(0) = new date
d(1) = new date
d(2) = new date
d(2) = d(1)
if d(1).ParseDate("12/25/02") = true then
msgbox("Shortdate: " & d(1).shortdate)
end if
msgbox("Long date d(0): " & d(0).longdate)
msgbox("Long date d(1): " & d(1).longdate)
msgbox("Long date d(2): " & d(2).longdate)
end sub
PACKING VALUES INTO A LIST
The LIST function allows you to pack values into a list. This function is very useful when you want to return multiple values or manipulate multiple values in a FOR-LOOP.
The syntax for the LIST function is as follows:
LIST( value1, value2, value3, valueN... ) as List
The list function takes N number of values each separated by a comma. A value can be a variable or a literal value like a number or a string. List will return an array of type LIST. If one of the arguments is a variable a referance to the actual variable is used instead of a copy.
Declare function getMultipleValues() as string
Sub Main()
DIM i as string
DIM r as list
r = getMultipleValues()
msgbox(r["name"] & " " & r["lname"] & " of age " & r["age"])
End Sub
Function getMultipleValues()
DIM name as string,lname as string
DIM age as integer
name = "Jose"
lname = "Cuevas"
age = 30
Return list(name,lname,age)
End Function
Declare Function getMultipleValues() as string
Sub Main()
DIM name AS string,lname AS string
DIM age AS integer
list(name,lname,age) = getMultipleValues()
msgbox(name & " " & lname & " of age " & age)
End Sub
Function getMultipleValues()
DIM name AS string, lname AS string
DIM age AS integer
name = "Jose"
lname = "Cuevas"
age = 30
Return list(name,lname,age)
End Function
PACKING NUMERIC VALUES USING RANGE
The RANGE function allows you to create a list of numeric values.
The syntax for the RANGE function is as follows:
RANGE( StartValue, EndValue ) as List
The RANGE function takes two numeric arguments. The first is the integer value where the list will start and the second is the value where the list will end. The list returned by range will have EndValue minus StartValue rows. The first row of the list will have a value of StartValue the second will have a value of StartValue plus one and so on until EndValue.
Sub Main()
DIM n AS string
FOR n in list("jose","joe","lourdes","cuevas")
msgbox(n)
NEXT
msgbox("Exit with value: " & n)
DIM i AS integer
FOR i IN list(5,6,7,8,9,10)
msgbox("Value of i: " & i)
NEXT
msgbox("Testing range")
FOR i IN range(30,45)
msgbox("Value of i: " & i)
NEXT
End Sub
|