SELECTINDEX, SELECTINDEXV

SELECTINDEX, SELECTINDEXV

Top  Previous  Next

 

The SELECTINDEX statement creates a numbered select list from an alternate key index entry. The SELECTINDEXV statement is similar but creates a select list variable.

 

 

Format

 

SELECTINDEX index.name {, value} FROM file.var {TO list.no}

 

SELECTINDEXV index.name {, value} FROM file.var TO list.var

 

where

 

index.nameis the name of the alternate key index to be processed.

 

valueis the value to be located in the index.

 

file.varis the file variable associated with an open file.

 

list.nois the select list number of the list to be created. If omitted, select list zero is used.

 

list.varis the select list variable to be created.

 

 

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 identified 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 an orders file keyed by order number for which there is an index on a field that contains the customer number. The index will have an entry for each customer who has placed an order and each of these entries will hold a list of the order numbers for that customer. The order of the indexed values (customer numbers, left column in the diagram below) will be sorted left or right aligned according to the format code in the dictionary entry for the customer number field. The order numbers stored for each customer (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. In this example, both items are likely to be defined as right aligned and hence sorted into ascending order.

 

index

 

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

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:

SETLEFT, SETRIGHT, SELECTLEFT, SELECTRIGHT