HPlogo Using KSAM XL and KSAM 64 > Chapter 5 Reading File Data

Sequential Access by Primary and Alternate Key

MPE documents

Complete PDF
Table of Contents
Index

E0300 Edition 4 ♥
E0394 Edition 3

Two intrinsics, FFINDN and FFINDBYKEY, can be used to set the logical pointer to the lowest value of an alternate key field. The FFINDN intrinsic identifies the first record by using a logical record number. The FFINDBYKEY intrinsic uses a key value to determine the first record.

When the first record has been located, the FREAD intrinsic reads the first record specified by the alternate key. Subsequent reads reposition the logical pointer and read the next logical record.

The FREADBYKEY intrinsic can also be used to position the logical pointer by alternate key value. In this case, however, the user must know the lowest value of the alternate key. An approximate value cannot be used with this intrinsic.

Specifying the Record Number


The FFINDN intrinsic positions the pointer to the record specified by the logical record number of the appropriate key. To position the pointer to the particular record of a key, the intrinsic parameters identify the particular key of interest and then the record number.

Depending on how the file was built, the first record of any key is identified by 1 or 0. Use option 5 or 7 of the LISTFILE command to determine how records are numbered in the file you are accessing. A negative record number also positions the pointer to the lowest value in the key field.

The key location identifies the key field to be used. Again, use option 5 or 7 of the LISTFILE command to determine the location of the desired key (ffn_key_location). The following example identifies the record of an alternate key and reads the specified record:

Figure 5-1 FFINDN Intrinsic Sample

  FFINDN(filenum,ffn_rec_number,ffn_key_location);
  .
  .
  .
  lgth :=FREAD(filenum,fr_record,fr_tcount);

Specifying a Key Value


The FFINDBYKEY intrinsic can also be used to position the pointer to an alternate key. This intrinsic is intended to position the pointer to the first occurrence of a record value that matches or is greater than the key value. This is referred to as an approximate match. To position the pointer to the first record of the key, supply a key value that is less than any value of the key and specify a relational operator of 1 (greater than) or 2 (equal to or greater than). For example, a relational operator of 1 locates the first record having a key value greater than the key value provided.

Figure 5-2 "FFINDBYKEY Intrinsic Sample" sets the pointer to the lowest value of the alternate key by searching for the first occurrence of a key value greater than (relop = 1) the value "0000":

Figure 5-2 FFINDBYKEY Intrinsic Sample

  fby_keyvalue   := '0000';
  fby_keylocation := 1;
  fby_keylength  := 4;
  fby_relop    := 1;
  .
  .
  .
  FFINDBYKEY(filenum,fby_keyvalue,fby_keylocation,fby_keylength,
    fby_relop);
  .
  .
  .
  lgth := FREAD(filenum,fr_record,fr_tcount);




Sequential Access by Primary Key


Sequential Access by Partial Key Value