Skip to main content
Previous sectionNext section

Arrays

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

  • Create sparse, multidimensional arrays.

  • Use any combination of integer, string, and numeric values are array subscripts. Such arrays are automatically sorted by subscript value; you never need to invoke a sort function.

  • Use arrays without having to declare their size and dimensions. You do not need to Dim or Redim arrays in Caché Basic.

  • Use a number of built-in commands for manipulating and traversing arrays.

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
Copy code to clipboard

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
Copy code to clipboard

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")
Copy code to clipboard

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))
Copy code to clipboard

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
Copy code to clipboard

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
Copy code to clipboard

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
Copy code to clipboard

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 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))
Copy code to clipboard

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
Copy code to clipboard

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
Copy code to clipboard