# System Functions

KGN( ) | Generate Record Key

## Format

KGN([ext_key$],data$,key_def$,key_num [ ,SEP=sepchar$ ] [,ERR=stmtref])

Where:

ext_key$ | Value of the external key. Optional. String expression.
data$ | Contents of the data record. String expression.
key_def$ | Key definition structure. String expression. This can be extracted using the FIB( ) function at position 85 (in native PxPlus mode) or using the FIN( ) function at position 86 for a length of 385 (in BBx® emulation mode).
key_num | Key number (primary or an alternate key) to extract. Numeric expression - Base 0 (primary key=0, 1st secondary=1, etc.).
sepchar$ | Separator character to be used in the parsing of data$. If omitted, the system separator is used or the Dynamic field separator logic is applied if enabled.
stmtref | Program line number or statement label to which to transfer control.

(The ability to have a SEP table and Dynamic separators was added in PxPlus v7.00.)

## Returns

Key of record in file, given the record's contents.

## Description

The KGN( ) function returns a string comprising the key of the record provided. This function can be used to determine the value of an alternate (or primary) key of a record, given the record's contents (and external key, if present).

When comparing keys with descending segments, an application can specify the key number as a negative value. In this case, the descending segments will be inverted so that a logical compare will function properly.

## Example

0010 open (13)"Cstfile"
0020 if prm('BX')=0 then X$=mid(fib(13),85) else X$=mid(fin(13),86,385)
0030 K$=key(13,end=0070)
0040 read record (13,key=K$)R$
0050 print "Key: ",K$," Alt: ",kgn(K$,R$,X$,1)
0060 goto 0030
0070 print "End-of-file"
0080 end

BBx® is a registered trademark of BASIS International Ltd.