READPREV.

Read a select list but in reverse order

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

Notes:

This new statement is syntactically similar to the READNEXT except it traverses the list in reverse order !

The following rules are obeyed, when the traverse direction is changed from forward to backward or vice-versa:

  • When a SELECT statement is first executed a forward direction is assumed. Therefore if a SELECT is immediately followed by a READPREV, then a change of direction is assumed.
  • During the READNEXT or READPREV sequence a next-key pointer is kept up to date. This is the record key, or index key to use should a READNEXT be executed.
  • During a change of direction from forward (READNEXT) to backward (READPREV) then the next record key or index key read in by the READPREV will be the one preceding the next-key pointer.
  • When the select list is exhausted it will either point one before the start of the select list (if READPREV’s have been executed) or one past the end of the select list (if READNEXT’s have been executed). Thus in the event of a change of direction the very first or very last index key or record key will be used.

Syntax:

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

Example:

Consider the following jBC code

list = "DAVE" : @AM : "GREG" : @AM : "JIM"
SELECT list

The following table shows what happens if you do READNEXT’s and READPREV’s on the above code and the reasons for it.

Statement Executed Result of Operation Comment
READNEXT key ELSE … key becomes "DAVE" First key in list
READNEXT key ELSE key becomes "GREG" Second key in list
READPREV key ELSE key becomes "DAVE" Direction reversed so we take the key that preceded the last one read in.
READPREV key ELSE The ELSE clause is taken The next-key pointer now exhausted at start of list
READNEXT key ELSE key becomes "DAVE" First key in list
READNEXT key ELSE key becomes "GREG" Second key in list
READNEXT key ELSE key becomes "JIM" Third and final key in list. The next-key pointer is now exhausted at end of list
READPREV key ELSE key becomes "JIM" Direction reversed by as the list was exhausted we don’t skip a key as we did in previous reversal of direction
READPREV key ELSE key becomes "GREG" Second key in list
READPREV key ELSE key becomes "DAVE" First key in list

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