Extensions to the READNEXT statement

  Return to Contents Page   Return to Knowledge Base   Return to JAC Home Page


The READNEXT statement has been extended in two ways to make use of secondary indexes.

Firstly it can now accept an index variable create with an OPENINDEX statement. An example of this is given in the description for SELECT extensions.

Secondly, you can use the KEY modifier as shown below.


READNEXT KEY index.key{,record.key{,vmcount}} {FROM select-def} THEN/ELSE


In this second format the READNEXT KEY statement will return the actual index key. The select used use MUST have been created with the OPENINDEX statement. You can optionally return the associated record key and the multi-value number associated with it.


The following code shows a file and index being created, a simple index just on attribute 1. The index is defined as multi-valued. Then 3 records are written to the file. The file is then interrogated with the OPENINDEX and READNEXT KEY statements. As the index is defined as an ascending left justified sort on attribute 1, the values returned with the READNEXT will be in sorted order of attribute 1.

* Create the file and index definition.
filename = "example"
indexname = "index.def"
EXECUTE "create-file ":filename:" 1 3"
EXECUTE "create-index -r ":filename:" ":indexname:" by M1"
OPEN filename TO filevar ELSE STOP 201,filename
* Open the index ready for use.
OPENINDEX filename,indexname TO index.var ELSE
    STOP 201,indexname:" IN ":filename
* Write some dummy records out.
OPEN filename TO filevar ELSE
    STOP 201,filename
WRITE "GREG" ON filevar,"customer-1"
WRITE "JIM":@VM:"DAVE" ON filevar,"customer-2"
WRITE "JIM":@VM:"GREG":@VM:"DAVE" ON filevar,"customer-3"
* Use SELECT to get ready to read in the records.
SELECT index.var
* Read in each index record at a time.
LOOP WHILE READNEXT KEY index.key,record.key,mv.key DO
   CRT index.key,record.key,mv.key

The output of this program will be as follows

DAVE customer-2 2
DAVE customer-3 3
GREG customer-1 1
GREG customer-3 2
JIM customer-2 1
JIM customer-3 1

The index is built on sorted attribute 1 and so the index keys (the names of the customers) are sorted in alphabetical order. They are then sub-sorted by record key and finally by the multi-value number that they existed in.

http://807199.827977/r5/knowledgebase/manuals/3.0/SecondaryIndexes/html/readnext.htm last modified on 06/19/09 05:29 AM