How to Use a UDF in Index with the Trim FunctionsID: Q129889 2.5x 2.6x 3.00 | 2.00 2.5x 2.6x | 2.5x 2.60x | 2.60
The information in this article applies to:
SUMMARYWhen creating an index key, it is important to remember that the index keys will have a fixed length even if the length of the field contents are varying lengths. FoxPro does not create or use variable-length keys. Index keys are padded with spaces to a constant size (the length of the field in the table structure). You can use a User-Defined Function (UDF) in an index expression. For example, you can order the records based on a partial field (for example, the street name in an address field). When a UDF is used to create an index key, the UDF is called twice to initiate the process before the records are processed. Then it is called once for each record in the table. This article shows by example how to use a UDF in an index expression with the four trim functions.
MORE INFORMATIONThe four trim functions, TRIM(), ALLTRIM(), RTRIM(), and LTRIM() are ignored during the first two passes through the UDF. Then they are used to generate the index keys. However, note that the resulting key is still padded with blanks to the constant length of the field in the index. It is still a fixed-length index. When tracing the UDF through the TRACE WINDOW, the first two passes through the code, as the index is built, calculate the key expressions so all the functions for removing blanks are ignored. The remaining passes through the UDF build the actual index keys. During this phase of building the index keys, the trim functions operate as expected within the UDF.
Sample CodeThis sample code creates a table (TEST.DBF) and a text file (UDFINDEX.TXT) containing output that shows the values of the record number and the string's length, using the ALLTRIM function. It also creates a second index and list to compare with the first list, showing that the ALLTRIM function did work in the UDF index. Notice the placement of the record with the name "Joe Brown." *Beginning of program. SET ALTERNATE TO udfindex.txt SET ALTERNATE ON CLEAR CREATE TABLE test (name C(30)) INSERT INTO test (name) VALUES ("John Doe") INSERT INTO test (name) VALUES ("Jane Smith") INSERT INTO test (name) VALUES (" Joe Brown") INSERT INTO test (name) VALUES ("Mary Green") CLEAR PUBLIC n n=1 INDEX ON strtrim(name) TAG testtag
m.name=test.name
* Note that the memory variable created from the field is again 30
* characters long. It has been padded with spaces to a constant length.
* Note that the leading spaces are ignored in this output showing that
* ALLTRIM is working.
LIST
INDEX ON name TAG name
* Note that the leading spaces are not ignored in this list.
LIST
SET ALTERNATE OFF
CLOSE ALL
MODIFY FILE udfindex.txt
FUNCTION strtrim PARAMETER strin IF LEN(ALLTRIM(strin)) = LEN(strin)
ELSE
ENDIF
? " Record number is: "+ALLTRIM(STR(RECNO()))
N=N+1
? " The trimmed string's length is: "+ALLTRIM(STR(LEN(ALLTRIM(strin))))
IF n = 3
ENDIF
RETURN ALLTRIM(strin)
* End of program. Additional reference words: VFoxWin FoxWin FoxMac FoxDos database design considerations 2.00 2.50 2.50a 2.50b 2.50c 2.60 2.60a 3.00 KBCategory: KBSubcategory: FxprgTable
|
Last Reviewed: October 17, 1998 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |