SELECTINDEX() |
|
|
The SELECTINDEX statement creates a select list from an alternate key index entry.
Format
SELECTINDEX index.name {, value} FROM file.var {TO list.no}
where
If the value is omitted, the SELECTINDEX statement constructs a select list containing all the values of the index identified by index.name. If the value is included, the SELECTINDEX statement constructs a select list containing keys of records for which the index indentifed by index.name has the given value.
Thus, in a file of orders with an index on the customer number field, the first form would return a list of customers referenced by the orders file and the second form would return a list of orders for a specific customer.
The STATUS() function returns zero if the SELECTINDEX is successful, non-zero if it fails because the index does not exist. Selecting records for a value that is not present in the index will return an empty list.
]The @SELECTED variable is set to the number of entries in the returned list.
The SELECTINDEX operation leaves the internal index pointer used by the SELECTLEFT and SELECTRIGHT statements positioned at the item that has been located or, if not found, at the position where such an item would go.
Use of this statement inside a transaction will not reflect any uncommitted updates to the file.
To understand the order in which items are returned by SELECTINDEX, consider a file keyed by customer number for which there is an index on a field that contains the dates of orders placed by the customer. The index will have an entry for each date and each of these entries will hold a list of customers placing orders on that date. The order of the indexed values (order dates, left column in the diagram below) will be sorted left or right aligned according to the format code in the dictionary entry for the date field. The customer numbers stored for each date (a single line from the right column in the diagram below) will be sorted according to the format code in the dictionary entry for the @ID item in the customers file.
Examples
SELECTINDEX 'CUST.NO' FROM ORDERS.FILE TO 7 LOOP READNEXT CUST.NO FROM 7 ELSE EXIT CRT CUST.NO SELECTINDEX 'CUST.NO', CUST.NO FROM ORDERS.FILE LOOP READNEXT ORDER.NO ELSE EXIT CRT ORDER.NO REPEAT
This program builds a select list of all the customers referenced by the orders file as list 7. The inner loop then constructs a list of the order numbers for each customer in turn.
See also: |