Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

AELEMENT(), ASUBSCRIPT()

These two functions convert between the two numbering schemes for array elements: continuous element numbering and row/column numbering. Because some array functions use one form and others need the other form, these functions are pretty useful.

AELEMENT() takes an array, a row and, optionally, a column and returns the corresponding element number. ASUBSCRIPT() takes an array and an element number and returns either the row or the column.

Usage

nElement = AELEMENT( ArrayName, nRow [, nCol ] )

Parameter

Value

Meaning

ArrayName

Array Name

The array in which you want to convert from row, column notation to element notation. It's necessary to specify the array because the shape of the array determines the conversion.

nRow

Numeric

The row number of the item for which the element number is desired.

nCol

Numeric

The column number of the item for which the element number is desired.

Omitted

Treat ArrayName as a one-dimensional array and return nRow.

nElement

Numeric

The element number of the specified element. If the array is one-dimensional or nCol is omitted, nRow is returned.

Example

DIMENSION aTest[5], a2DTest[4,2]
? AELEMENT(aTest, 3)        && returns 3
? AELEMENT(a2DTest, 3)      && returns 3
? AELEMENT(a2DTest, 3, 2)   && returns 6

Usage

nSubscript = ASUBSCRIPT( ArrayName, nElement, nSubscript )

Parameter

Value

Meaning

ArrayName

Array Name

The array in which you want to convert from element notation to row, column notation. It's necessary to specify the array because the shape of the array determines the conversion.

nElement

Numeric

The element number of the item for which either the row or column number is desired.

nSubscript

1

Return the row subscript of the element. In a one-dimensional array, this is the same as the element number.

2

Return the column subscript of the element. In a one-dimensional array, gives an error message.

ASUBSCRIPT() is particularly useful when working with ASCAN(), which returns an element number. We often want to get a particular element out of the same row of the array as the element found by ASCAN(). ASUBSCRIPT() gives us the row number, which we can then use to grab the relevant item.

Watch out for one thing. When nElement isn’t a valid element number for the array, you get an error. Frankly, we think we’d prefer if ASUBSCRIPT() returned 0 in that case and let us deal with it.

Example

DIMENSION aTest[5],a2DTest[4,2]
? ASUBSCRIPT(aTest, 3, 1)   && returns 3
? ASUBSCRIPT(a2DTest, 3, 1) && returns 2
? ASUBSCRIPT(a2DTest, 3, 2) && returns 1
* The next example assumes SearchItem has a value
* that can be found in the array.
? ASUBSCRIPT(a2DTest, ASCAN(a2DTest, SearchItem), 1)

See Also

ALen(), Array Manipulation, AScan()