Using Caché Basic
Arrays
[Back] [Next]
   
Server:docs1
Instance:LATEST
User:UnknownUser
 
-
Go to:
Search:    

Caché Basic includes much more powerful support for arrays than other versions of Basic. Specifically, with Caché Basic you can:

In addition, you can create and work with persistent multidimensional arrays (globals) that are automatically stored within the database. For more information, refer to Using Caché Globals.
Defining Arrays
You do not need to declare array variables in any special way. To create an array, you simply place values into it:
' Create an array
arr(1) = "First element"
arr(2) = "Second element"
arr(3) = "Third element"

' Display the contents of the array
For i = 1 To 3
    PrintLn i & ": " & arr(i)
Next
 
Each array element (node) can contain up to 32K of data.
To create a multidimensional array, simply use more subscripts:
' Create a multidimensional array
arr(1,1) = "X" : arr(1,2) = "O" : arr(1,3) = "X"
arr(2,1) = "X" : arr(2,2) = "X" : arr(2,3) = "O"
arr(3,1) = "O" : arr(3,2) = "O" : arr(3,3) = "X"

' Display the contents of the array
For row = 1 To 3
    For col = 1 To 3
        Print arr(row,col) & " "
    Next col
    PrintLn
Next row
 
You can use strings as array subscripts:
' Create an associative array
notes("Jack") = "Fell down; Broke crown"
notes("Jill") = "Tumbled after"

' Display the contents of the array
PrintLn "Jack: " & notes("Jack")
PrintLn "Jill: " & notes("Jill")
 
Existence Testing
You can test if an array node (or any variable) is defined (has a value) or has sub-nodes using the Exists function. The Exists function returns one of the following possible values:
Exists Return Values
Constant Value Meaning
vbUndefined 0 Variable is undefined.
vbHasValue 1 Variable has a value.
vbHasArray 2 Variable is not defined but has subnodes.
vbHasValue AND vbHasArray 3 Variable is defined and has subnodes.
For example:
' Initialize
var = "Variable"
arr(1) = "Array node with subnode"
arr(1,1) = "Array node"

' Test for existence
PrintLn "Exists(a)        " & Exists(a)
PrintLn "Exists(var)      " & Exists(var)
PrintLn "Exists(arr)      " & Exists(arr)
PrintLn "Exists(arr(1))   " & Exists(arr(1))
PrintLn "Exists(arr(1,1)) " & Exists(arr(1,1))
 
Deleting Arrays
You can delete an array (its subscripts and values) using the Erase command:
array = "root node"
array("subnode") = "subnode"
array("subnode", "subnode") = "subnode, subnode"
Println Exists(array) 'returns 3; variable defined and has array elements
Erase array 
Println Exists(array) 'returns 0
 
Traversing Arrays
There are a number of ways to traverse an array to find what subscripts and values it contains.
Traversing with a For...Next Loop
If an array is subscripted by integer values and you know how many elements it contains, you can use a simple For...Next loop to list its contents:
' Create an array
For i = 1 To 10
    arr(i) = "Item " & i
Next i

' Display the contents of the array
For i = 1 To 10
    PrintLn i & ": " & arr(i)
Next
 
Traversing with a For Each...Next Loop
You can use a For Each...Next loop to list all the subscripts for an array:
' Create an array
food("Apple") = "Fruit"
food("Banana") = "Fruit"
food("Asparagus") = "Vegetable"
food("Broccoli") = "Vegetable"

' Display the array subscripts
For Each key In food
    PrintLn key
Next
 
The Traverse Function
You can use the Traverse function to find the next (or previous) subscript at any point within an array. (Traverse is the Basic equivalent of the Caché ObjectScript $ORDER function).
The Traverse function takes an array reference and returns the next (or previous) subscript at the specified subscript level. The specified subscript level is simply the last subscript listed in the array reference. For example:
' Define an array
arr("a",1) = "a1"
arr("a",2) = "a2"
arr("b",1) = "b1"
arr("b",2) = "b2"

PrintLn Traverse(arr(""))
PrintLn Traverse(arr("a"))
PrintLn Traverse(arr("b"))

PrintLn Traverse(arr("a",""))
PrintLn Traverse(arr("a",1))
 
We can use Traverse in a While loop to find all of the subscripts of the food array from the previous section:
' Create an array
food("Apple") = "Fruit"
food("Banana") = "Fruit"
food("Asparagus") = "Vegetable"
food("Broccoli") = "Vegetable"

' Display the array subscripts
key = Traverse(food("")) ' find first subscript
While (key <> "")
    PrintLn key
    key = Traverse(food(key)) ' find next subscript
Wend
 
To find previous nodes with Traverse, use the optional direction flag as a second argument:
' Create an array
food("Apple") = "Fruit"
food("Banana") = "Fruit"
food("Asparagus") = "Vegetable"
food("Broccoli") = "Vegetable"

' Display the array subscripts backwards
key = Traverse(food(""),-1) ' find last subscript
While (key <> "")
    PrintLn key
    key = Traverse(food(key),-1) ' find previous subscript
Wend