HP RPG/XL Programmer's Guide (30318-90001)

3- 11
Column 16 is D to indicate that MASTFL is a demand file.
Column 28 is L to specify that MASTFL will be processed within
key limits.
Columns 29-30 contain 15 to specify the length of the secondary
key (last name).
Column 31 contains A to specify that the secondary key is an
alphanumeric key.
Column 32 contains I to specify that this is a KSAM file.
2 This line specifies the starting secondary key value for reading
MASTFL.
Columns 28-32 are SETLL to position the file pointer to the
first record having a key equal to the value placed in the NCHAR
field.
3 This line reads the next record in MASTFL. READ is included in a
loop that processes only those records whose last name starts
with a specific character.
Columns 28-32 are READ to specify the Read key operation.
Reading a KSAM File Randomly. This section explains how to randomly
access records in a KSAM file by one of its key fields. There are two
ways to do this. The first method lets you supply the key value
dynamically in your RPG program. Use it when the key values are computed
by the program or when the program is run interactively. The second
method lets you enter the key values ahead of time into a RAF. RPG
accesses records in the KSAM file that match key values in the RAF. Use
the RAF method when you know what the key values are or when they can be
generated automatically by other programs. Examples of both of these
methods are included in the next two sections.
The following lines show how to use KSAMUTIL to create a KSAM file with
one (primary) key. The KSAM file is called MASTFL and duplicate keys are
not allowed:
:RUN KSAMUTIL.PUB.SYS
>BUILD MASTFL;REC=-256,4,F,ASCII;DISC=20000,20,4;KEYFILE=MASTFLK;&
>KEY=B,1,4
Specifying the Key Dynamically
If the records that you need to access in the KSAM file vary from one
program run to the next, you probably need to supply the record keys
dynamically. That is, you must retrieve or calculate them in the RPG
program before reading the KSAM file.
To process a KSAM file randomly by key, enter both a File Description and
a Calculation Specification similar to those shown in Figure 3-9. See
the KSAMUTIL commands in the previous section, "Reading a KSAM File
Randomly" for information on how this KSAM file is created.