Operator [ ] Foundation
Index operator (binary): accesses a specific element of an array, string, numeric or object.
<aArray>[<nElement,...>]
<aArray>[<nElement1>][<nElement2>] [...]
<nValue>[<nElement>]
<cString>[<nElement>]
<oObject>[<nElement>]
Individual elements of an array are accessed with the array operator. The expression returning the array must always precede the operator.
The numeric expression <nElement> must return an integer number not equal to zero in the range between the positive and negative number of array elements in the applicable dimension. A runtime error is generated if the value of <nElement> lies outside this range. If <nElement> has a positive value array elements are accessed from the beginning of the array. Negative values for <nElement> access an array from the end (the expression aArray[-1] == ATail(aArray) yields .T.). When <aArray> is an empty array the index operator cannot be used.
Extended functionality of the index operator
The index operator is not restricted to values of data type Array, but can also be used for character strings or numeric values.
Character
In case of character strings, the operator retrieves a single character of a string at the position <nElement> (see example below). This is much faster than retrieving a single character with the SubStr() function.
Numeric
When the operand has a numeric value, the operator tests if the bit at the position <nElement> is set. The result of this operation is .T. (true) when the bit is set, otherwise it is .F. (false). Individual bits of a numeric value can be manipulated by assigning .T. or .F. for the bit at the position <nElement>. The range for <nElement>extends from 1 to 32 when the operand is of numeric data type.
// This example demonstrates various possibilities for
// the index operator
PROCEDURE Main
LOCAL aArray := { "A", "B", "C" }
LOCAL i, j, nMax
// one dimensional array
? aArray[1] // result: A
? aArray[2] // result: B
? aArray[3] // result: C
aArray := Directory() // two dimensional array
nMax := Len( aArray )
FOR i := 1 TO nMax // output all elements
? aArray[i,1]
FOR j := 2 TO Len( aArray[i] )
?? aArray[i][j]
NEXT
NEXT
USE Customer NEW // combine function call
// with array element operator
? DbStruct()[1,1] // result: CUSTOMERNR
RETURN
// This example shows how the index operator can be
// used with values of data type Character.
PROCEDURE Main
LOCAL cString := "Xbase++ is cool"
LOCAL i, nLen, cTemp
// Traditional way
? SubStr( cString, 3, 1 ) // result: a
// Fast way
? cString[3] // result: a
cTemp := ""
i := 0
nLen := Len(cString)
DO WHILE ++i <= nLen // Strip all vowels
IF cString[i] $ "aeiou"
LOOP
ENDIF
cTemp += cString[i]
ENDDO
? cTemp // result: Xbs++ s cl
cTemp[3] := "a"
cTemp[4] := "s"
cTemp[5] := "e"
? cTemp // result: Xbase s cl
RETURN
// This example shows how the index operator can be
// used with values of data type Numeric.
PROCEDURE Main
LOCAL n
? n := 6 // result: 6
? n[1] // result: .F. (bit for 2^0 not set)
? n[2] // result: .T. (bit for 2^1 is set)
? n[3] // result: .T. (bit for 2^2 is set)
n[1] := .T. // setting bit for 2^0
? n // result: 7
RETURN
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.