HP 3000 Manuals

Logical Files (continued) [ HP COBOL II/XL Programmer's Guide ] MPE/iX 5.0 Documentation


HP COBOL II/XL Programmer's Guide

Logical Files (continued) 

Random Access Files 

A random access file is so named because any record can be accessed at
any time by its key.  The key corresponds to the record number minus one
(for example, key zero is the first record and key four is the fifth
record).

The MPE XL operating system does not distinguish between random access
and sequential organization files.  Therefore, a random access file can
be treated like a sequential organization file.

If it is accessed randomly, a random access file has these advantages
over a sequential organization file:

   *   Its records can be accessed in any order.
   *   New records can be inserted between existing records (as well as
       appended to the end of the file until the file is full, if there
       is room in key order).
   *   Its records can be updated.

A random access file has these disadvantages over a sequential
organization file:

   *   It is not ANSI standard (it is a carryover for COBOL 68
       compatibility)
   *   It must reside on a disk.
   *   It limits the portability of the programs that access it randomly.
       (They cannot run on systems that do not have disk storage on which
       the file can reside, for example.)
   *   It requires space for every possible record, rather than only for
       records that are actually written to it.[REV BEG]
   *   Requires fixed length records.[REV END]

A random access file has these advantages over a relative organization
file:

   *   It is a standard MPE file.
   *   Non-COBOL programs and routines can read it.
   *   Access is much faster.  WRITE works on a random access file record
       whether it has been written before or not.  On a relative
       organization file, WRITE only works on new records.  Overwriting
       requires REWRITE.

A random access file has these disadvantages over a relative organization
file:

   *   It is not ANSI standard.
   *   Its records cannot be deleted.

A random access file can be opened for input, write-only, or input-output
access.  It is appropriate for a program that must skip around in the
file.  An example is a record file in which a few random records are
updated at a time.

How to Code Random Access Files.   

The code that your program needs in order to perform input and output
with random access files is the following:

   1.  In the ENVIRONMENT DIVISION, a SELECT statement with an ASSIGN
       clause for each file.  The clauses ACCESS MODE IS RANDOM and
       ACTUAL KEY are required.

   2.  In the FILE SECTION of the DATA DIVISION, an FD entry to match
       each SELECT statement, with an 01 record for each file.

   3.  In the FILE SECTION or WORKING STORAGE SECTION of the DATA
       DIVISION, a definition of each data name that you specified in an
       ACTUAL KEY clause in the ENVIRONMENT DIVISION. Define each data
       item as an integer large enough to number every record in the
       file.  For maximum efficiency, use PIC S9(9) COMP SYNC.

   4.  In the PROCEDURE DIVISION, procedures to OPEN, READ, WRITE,
       REWRITE, and CLOSE the files, and to assign keys (record numbers).

Example.   

The following program uses random access files.

     001000 IDENTIFICATION DIVISION.
     002000 PROGRAM-ID. RANDCRSC.
     003000* This program creates a simple random file to show the
     004000* blank record created.
     005000 ENVIRONMENT DIVISION.
     006000 INPUT-OUTPUT SECTION.
     007000 FILE-CONTROL.
     008000     SELECT RANDFILE ASSIGN TO "RANDFILE"
     009000                     ACCESS IS RANDOM
     009100                     ACTUAL KEY IS RANDKEY.
     010000
     011000 DATA DIVISION.
     012000 FILE SECTION.
     013000 FD RANDFILE.
     014000 01 RANDREC.
     016000      03   FILLER        PIC  X(10).
     016100 WORKING-STORAGE SECTION.
     016200 01 RANDKEY    PIC 999.
     017000 PROCEDURE DIVISION.
     018000 P1.
     019000     OPEN OUTPUT RANDFILE.
     020000     MOVE 0 TO RANDKEY.
     020100     MOVE "000" TO RANDREC.
     021000     WRITE RANDREC
     022000           INVALID KEY PERFORM ERROR-RTN.
     023000     MOVE 6 TO RANDKEY.
     023100     MOVE "006" TO RANDREC.
     024000     WRITE RANDREC
     025000           INVALID KEY PERFORM ERROR-RTN.
     026000     MOVE 2 TO RANDKEY.
     026100     MOVE "002" TO RANDREC.
     027000     WRITE RANDREC
     028000           INVALID KEY PERFORM ERROR-RTN.
     029000     CLOSE RANDFILE.
     030000     STOP RUN.
     031000 ERROR-RTN.
     032000     DISPLAY "KEY ERROR" RANDREC.
     033000     STOP RUN.

The preceding program creates a temporary random access file named
RANDFILE. The following command displays information about RANDFILE:

     :LISTFTEMP RANDFILE,2

Following is the information displayed:

     FILENAME  CODE  ------------LOGICAL RECORD------------  ----SPACE----
                       SIZE  TYP        EOF      LIMIT  R/B  SECTORS #X MX

     RANDFILE           10B  FA           7      10000   25       16  1  *

The following command displays the contents of RANDFILE:

     :PRINT RANDFILE

The above command displays the following:

     000     Record 1
             Record 2 (blank)
     002     Record 3
             Record 4 (blank)
             Record 5 (blank)
             Record 6 (blank)
     006     Record 7

Assigning Values to Keys.   

The PROCEDURE DIVISION must include a procedure that assigns a key to the
data item specified by the ACTUAL KEY clause for each record in the file.
When you write this key-assigning procedure, keep in mind:

   *   The first record must receive the number zero.

   *   It is an error to assign a record number that is greater than the
       number of records the file can hold.  (This number is determined
       when the file is created with the FILE or BUILD command.)

   *   A random access file is allocated space for every possible key, so
       avoid assigning keys in a way that creates a lot of unused space.
       For example, if you assign keys 1000-9999, dummy records 0-999
       will be allocated.  If you never intend to use records 0-999, they
       waste space.

   *   If the keys do not naturally fall within record number zero and
       the last record in the file, you must devise an algorithm to
       compute the record key -- a hashing algorithm, for example.

A hashing algorithm is any algorithm that maps larger numbers to smaller
numbers.  The following are some characteristics of hashing algorithms:

   *   Hashing algorithms often use modular arithmetic.

   *   They add the digits of a larger number together to produce a
       smaller number.

   *   They map nonnumeric data to numbers and then map those numbers to
       smaller numbers.

For more information on hashing algorithms, refer to a book on computer
algorithms.  For an example of a hashing algorithm, see the example in
"Relative Organization Files."

Accessing Random Access Files Sequentially.   

The MPE XL operating system does not distinguish between random access
and sequential organization files.  The distinction is made by the HP
COBOL II/XL compiler, which generates different code for random access
files.  This means that a file can be created as a sequential
organization file by one program and treated as a random access file by
another program, or vice versa.

When a random access file is treated as a sequential organization file,
its records are accessed sequentially, beginning with record zero.  The
file system cannot distinguish between dummy records and real records,
however.  If the random access file is an ASCII file, its dummy records
contain spaces; if it is a binary file, its dummy records contain binary
zeros.  Dummy records are treated as data records.

Relative Organization Files 

A relative organization file is so named because the relative order of
its records is constant.  It has MPE XL file type RIO. It differs from a
random access file only in the following ways:

   *   Its first key is one, not zero.

   *   Its records can be deleted.

   *   It is portable to MPE computers (not completely portable).

   *   It does not limit the portability of the programs that use it,
       because it is ANSI Standard.

   *   In addition to space for all possible records, it requires space
       for one tag per record (the tag indicates whether the associated
       record has been deleted).

   *   It is not interchangeable with a sequential organization file.
       That is, it cannot be created as a sequential file by one program
       and accessed as a relative organization file by another, or vice
       versa.  The reason is that a relative organization file has an
       extra bit allocated for each record.  Each bit indicates whether
       its record has been deleted.

   *   It uses disk space less efficiently than a random access file.

   *   I-O is less efficient than it is on a random access file.[REV BEG]

   *   Simulates variable length records (with fixed length records)
       transparently.[REV END]

How to Code Relative Organization Files.   

The code that your program needs to perform input and output with
relative organization files depends on how you want to access the file.
You can access a relative organization file sequentially, randomly, or
dynamically.

Sequential Access.   

Sequential access is the default.  You may specify ACCESS IS SEQUENTIAL
in the SELECT statement or omit it.  Sequential access allows you to
access records in order.  Use sequential access when you want to move
forward in the file, but never backward.  For example, a batch payroll
program that processes every employee record would use sequential access.

Random Access.   

Random access requires that you specify ACCESS IS RANDOM in the SELECT
statement.  Random access allows you to access records by key.  Use
random access when you want to access records in any order.  For example,
a personnel application program that retrieves employee records by
employee number would use random access.

Dynamic Access.   

Dynamic access requires that you specify ACCESS IS DYNAMIC in the SELECT
statement.  Dynamic access allows you to skip to any record, before or
after the current record, and then access the file sequentially from
there.  Use dynamic access when you want to access records in order after
a certain record.  For example, a program that reads every record written
after a certain date, where the date is the key, would use dynamic
access.

Table 5-4  lists the access modes for relative organization files, the
open modes associated with them, the I-O statements that are[REV BEG]
valid with those open modes, and the phrases that are valid and
invalid[REV END] with those I-O statements.

          Table 5-4.  Acess Modes, Open Modes, and Valid I-O Statements 
                      for Relative Organization Files 
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
|    Access Mode    |     Open Mode     | Valid Statements  |   Valid Phrases   |  Invalid Phrases  |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
| Sequential        | INPUT             | READ              | NEXT, INTO, AT    | INVALID KEY, NOT  |
|                   |                   |                   | END, NOT AT END,  | INVALID KEY       |
|                   |                   |                   | END-READ          |                   |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
|                   |                   | START             | All phrases.      | None.             |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
|                   | OUTPUT            | WRITE             | All phrases.      | None.             |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
|                   | I-O               | READ              | NEXT, INTO, AT    | INVALID KEY, NOT  |
|                   |                   |                   | END, NOT AT END,  | INVALID KEY       |
|                   |                   |                   | END-READ          |                   |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
|                   |                   | REWRITE           | FROM, END-REWRITE | INVALID KEY, NOT  |
|                   |                   |                   |                   | INVALID KEY       |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
|                   |                   | START             | All phrases.      | None.             |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
|                   |                   | DELETE            | END-DELETE        | INVALID KEY, NOT  |
|                   |                   |                   |                   | INVALID KEY       |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
|                   | EXTEND            | WRITE             | All phrases.      | None.             |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------

          Table 5-4.  Acess Modes, Open Modes, and Valid I-O Statements 
                      for Relative Organization Files (cont.) 
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
|    Access Mode    |     Open Mode     | Valid Statements  |   Valid Phrases   |  Invalid Phrases  |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
| Random            | INPUT             | READ              | INTO, INVALID     | AT END, NOT AT    |
|                   |                   |                   | KEY, NOT INVALID  | END               |
|                   |                   |                   | KEY, END-READ     |                   |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
|                   | OUTPUT            | WRITE             | All phrases.      | None.             |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
|                   | I-O               | READ              | INTO, INVALID     | AT END, NOT AT    |
|                   |                   |                   | KEY, NOT INVALID  | END               |
|                   |                   |                   | KEY, END-READ     |                   |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
|                   |                   | REWRITE, DELETE,  | All phrases.      | None.             |
|                   |                   | WRITE             |                   |                   |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
| Dynamic           | INPUT             | READ              | All phrases.      | None.             |
|                   |                   |                   | (NEXT is required |                   |
|                   |                   |                   | to show that      |                   |
|                   |                   |                   | records are to be |                   |
|                   |                   |                   | accessed          |                   |
|                   |                   |                   | sequentially.)    |                   |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
|                   |                   | START             | All phrases.      | None.             |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
|                   | OUTPUT            | WRITE             | All phrases.      | None.             |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------
|                   |                   |                   |                   |                   |
|                   | I-O               | READ, REWRITE,    | All phrases.      | None.             |
|                   |                   | START, DELETE,    |                   |                   |
|                   |                   | WRITE             |                   |                   |
|                   |                   |                   |                   |                   |
-----------------------------------------------------------------------------------------------------

As with a random access file, a relative organization file may require
that the program have a hashing algorithm.

Example.   

This program writes records to a relative file, using a simple hashing
algorithm to map Social Security numbers to three-digit numbers.  The
hashing algorithm uses the last three digits of the Social Security
number as the key.  If there is a duplicate key, the hashing algorithm
adds one to the key until it is unique.

     001000 IDENTIFICATION DIVISION.
     002000 PROGRAM-ID. HASHSC.
     003000* This program writes records to a relative file using
     004000* a hashing scheme.
     005000
     006000 ENVIRONMENT DIVISION.
     007000 INPUT-OUTPUT SECTION.
     008000 FILE-CONTROL.
     009000     SELECT RELFILE  ASSIGN TO "RELFILE"
     010000                     ORGANIZATION IS RELATIVE
     011000                     ACCESS IS RANDOM
     012000                     RELATIVE KEY IS W-KEY.
     013000
     014000 DATA DIVISION.
     015000 FILE SECTION.
     016000 FD RELFILE.
     017000 01 REL-REC.
     018000     03  REL-SS-NO      PIC  X(9).
     019000 WORKING-STORAGE SECTION.
     020000 01  IN-SS-NO.
     021000     88  NO-MORE-SS-NUMBERS  VALUE ALL "9".
     022000     05  FILLER         PIC X(6).
     023000     05  IN-KEY         PIC 999.
     024000 01  WRITE-SWITCH       PIC XXX.
     025000     88  SUCCESS        VALUE "YES".
     026000     88  RESET-SWITCH   VALUE "NO".
     027000 01  W-KEY              PIC 999.
     028000

     029000 PROCEDURE DIVISION.
     030000 000-MAIN-PROG.
     031000     OPEN OUTPUT RELFILE.
     032000     PERFORM 100-GET-SS
     033000     PERFORM WITH TEST AFTER UNTIL NO-MORE-SS-NUMBERS
     034000        PERFORM 200-WRITE-TO-RELFILE UNTIL SUCCESS
     035000        SET RESET-SWITCH TO TRUE
     036000        PERFORM 100-GET-SS
     037000     END-PERFORM
     038000     CLOSE RELFILE.
     039000     STOP RUN.
     040000
     041000 100-GET-SS.
     042000     ACCEPT IN-SS-NO.
     043000
     044000 200-WRITE-TO-RELFILE.
     045000     MOVE IN-KEY TO W-KEY
     046000     MOVE IN-SS-NO TO REL-SS-NO
     047000     PERFORM UNTIL SUCCESS
     048000         WRITE REL-REC
     049000             INVALID KEY     ADD 1 TO W-KEY
     050000             NOT INVALID KEY SET SUCCESS TO TRUE
     051000         END-WRITE
     052000     END-PERFORM.
     053000
     054000 999-LAST-PARA.
     055000     DISPLAY "ERROR -- FELL OFF THE END OF THE PROGRAM".

This program creates a file that contains the following data:

     Social security number 333444001 is record number 001
     Social security number 123423007 is record number 007
     Social security number 111222008 is record number 008
     Social security number 123456009 is record number 009
     Social security number 222333007 is record number 010
     Social security number 444555010 is record number 011

Indexed Organization Files 

An indexed file is so named because each record has an index, which is an
alphanumeric key.  An indexed file is organized as an MPE XL KSAM file is
organized.[REV BEG] You can access both Native Mode and Compatibility
Mode KSAM files from HP COBOL II/XL programs.[REV END] Refer to Using 
KSAM/XL and the KSAM/3000 Reference Manual for details.

An indexed file differs from a relative organization file only in the
following ways:

   *   The primary keys of an indexed file can be alphanumeric and must
       be written in ascending order (according to ASCII value) if access
       mode is sequential and open mode is OUTPUT or EXTEND. The primary
       keys must be USAGE DISPLAY.

   *   An indexed file can specify one to 16 keys.

   *   The indexed file's first key can be any value.

   *   Its keys need not be unique.  Two records can have the same value
       for one key only if they have different values for another key.
       It is recommended that primary keys be unique.[REV BEG]

   *   Two files are required for support of Compatibility Mode KSAM
       files, one for data and one for the index.  The data file has
       space only for records written, not for every possible record.
       For Native Mode KSAM files, only one file is required.[REV END]

An indexed file is appropriate when alphanumeric keys are appropriate or
keys are not unique.  An example is an employee file where the employees'
surnames are the keys.  The surnames are alphanumeric and two or more
employees can have the same surname.

An indexed file is also appropriate for a sparse file.  If you write a
record with the index "A" followed by a record with the index "Z" space
is not allocated for the records that could be inserted between them.

How to Code Indexed Organization Files.   

Indexed files can be accessed sequentially, randomly, or dynamically.
The code that your program needs to access indexed files sequentially is
the following:

   1.  In the ENVIRONMENT DIVISION, a SELECT statement with an ASSIGN
       clause for each file.  The clauses RECORD KEY and ORGANIZATION IS
       INDEXED are required.

   2.  In the FILE SECTION of the DATA DIVISION, an FD entry to match
       each SELECT statement, with an 01 record for each file.  In the FD
       entry, none of the clauses are required.  The 01 record must
       contain the data item specified in the RECORD KEY clause in the
       ENVIRONMENT DIVISION.

   3.  In the PROCEDURE DIVISION, procedures to OPEN, READ, WRITE,
       REWRITE, DELETE, and CLOSE the files, and to assign keys, or
       record numbers.

To access indexed files randomly or dynamically, add the clause ACCESS
MODE IS RANDOM or ACCESS MODE IS DYNAMIC to the SELECT statement.

Example.   

The following program uses an indexed organization file:

     001000 IDENTIFICATION DIVISION.
     002000 PROGRAM-ID. INDEXSC.
     003000* This program reads an indexed file dynamically.  It does a
     004000* random read to get to the specific ALUM record desired, then
     005000* it reads sequentially forward through the records of that alumnus
     006000* to total all the gifts made by that alumnus.
     007000 ENVIRONMENT DIVISION.
     008000 INPUT-OUTPUT SECTION.
     009000 FILE-CONTROL.
     010000     SELECT ALUMFILE ASSIGN TO "ALUMFILE"
     011000            ORGANIZATION IS INDEXED
     012000            RECORD KEY IS ID-NUMBER WITH DUPLICATES
     013000            ACCESS MODE IS DYNAMIC.
     014000     SELECT PRINT-FILE ASSIGN TO "PFILE".
     015000 DATA DIVISION.
     016000 FILE SECTION.
     017000 FD ALUMFILE.
     018000 01 ALUM-REC.
     019000      03 ID-NUMBER       PIC X(9).
     020000         88 NO-MORE-ALUMS VALUE "//".
     021000      03 NAME            PIC X(20).
     022000      03 GIFT            PIC S9(6)V99.
     023000      03 FILLER          PIC X(43).
     024000 FD PRINT-FILE.
     025000 01 PRINT-REC.
     026000      03 P-ID-NUMBER          PIC X(9).
     027000      03                      PIC X.
     028000      03 P-NAME               PIC X(20).
     029000      03                      PIC X.
     030000      03 P-TOTAL-GIFTS        PIC $$,$$$,$$$.$$.
     031000 WORKING-STORAGE SECTION.
     032000 01 EOF-SW               PIC X   VALUE "N".
     033000    88 ALUM-EOF          VALUE "Y".
     034000 01 W-TOTAL              PIC S9(7)V99 VALUE 0.
     035000 01 HOLD-ALUM.
     036000      03 H-ID-NUMBER     PIC X(9).
     037000      03 H-NAME          PIC X(20).
     038000      03 H-GIFT          PIC S9(6)V99.
     039000      03 FILLER          PIC X(43).

     040000 PROCEDURE DIVISION.
     041000 000-MAIN-PROG.
     042000     OPEN INPUT ALUMFILE OUTPUT PRINT-FILE.
     043000     PERFORM UNTIL NO-MORE-ALUMS
     044000         PERFORM 100-WHICH-ALUM
     045000         IF NOT NO-MORE-ALUMS
     046000             PERFORM 150-TOTAL-THE-ALUMS-GIFTS
     047000             END-IF
     048000     END-PERFORM.
     049000     STOP RUN.
     050000
     051000 100-WHICH-ALUM.
     052000     ACCEPT ID-NUMBER.
     053000
     054000 150-TOTAL-THE-ALUMS-GIFTS.
     055000     READ ALUMFILE INTO HOLD-ALUM
     056000         KEY IS ID-NUMBER
     057000         INVALID KEY PERFORM 200-NO-SUCH-ALUM
     058000         NOT INVALID KEY
     059000             MOVE ZERO   TO W-TOTAL
     060000             PERFORM UNTIL H-ID-NUMBER NOT = ID-NUMBER
     061000                 ADD GIFT TO W-TOTAL
     062000                 PERFORM 175-SEQUENTIAL-READ
     063000             END-PERFORM
     064000         END-READ.
     065000     PERFORM 300-PRINT-OUT-ALUM.
     066000
     067000 175-SEQUENTIAL-READ.
     068000     READ ALUMFILE NEXT RECORD
     069000         AT END MOVE ALL "X" TO ID-NUMBER
     070000         END-READ.
     071000 200-NO-SUCH-ALUM.
     072000     MOVE "                  THIS ID NUMBER NOT IN ALUMNI FILE"
     073000           TO PRINT-REC
     074000     MOVE ID-NUMBER TO P-ID-NUMBER
     075000     WRITE PRINT-REC.
     076000
     077000 300-PRINT-OUT-ALUM.
     078000     MOVE H-ID-NUMBER TO P-ID-NUMBER
     079000     MOVE H-NAME TO P-NAME
     080000     MOVE W-TOTAL TO P-TOTAL-GIFTS
     081000     WRITE PRINT-REC.

Creating Indexed Files.   

If your program is to use an indexed file that does not exist, HP COBOL
II/XL[REV BEG] (Native Mode) by default creates a temporary Native Mode
KSAM file--one file.  If an indexed file has variable length records, HP
COBOL II/XL creates a temporary Compatibility Mode KSAM file.

HP COBOL II/V (Compatibility Mode) by default creates[REV END] a
temporary Compatibility Mode KSAM file.  Compatibility Mode KSAM files
consist of two temporary files:  a data file and an index file.  The data
file has the name that the ASSIGN clause specifies.  The index file has
the same name, with "K" appended to it.  If the data file name has eight
characters, the index file name has the same first seven characters and
"K" as the eighth character.  In the SELECT statement, include the clause
ACCESS MODE IS SEQUENTIAL.
[REV BEG]

You can force an HP COBOL II/V program to create a Native Mode KSAM file
by using a FILE equation with the ";KSAMXL" option.[REV END]

Alternatively, you can create an indexed file before you execute your
program.[REV BEG] To create a Compatibility Mode KSAM file, use the
>BUILD command of the KSAM utility KSAMUTIL. To create a Native Mode KSAM
file, use the MPE XL :BUILD command with the ";KSAMXL" option.  Both HP
COBOL II/V and HP COBOL II/XL programs can access existing Native Mode
KSAM files and existing Compatibility Mode KSAM files.[REV END] For
details, refer to Using KSAM/XL and the KSAM/3000 Reference Manual.

Example.  [REV BEG]

If the following program is compiled with HP COBOL II/XL, the run-time
library creates the Native Mode KSAM file NFILE. If the program is
compiled with HP COBOL II/V, the run-time library creates a Compatibility
Mode KSAM file consising of[REV END] two temporary files, NFILE and
NFILEK. The program copies the records from the sequential file to the
indexed file, using the first six characters in each record as its key.

     001100 IDENTIFICATION DIVISION.
     001200 PROGRAM-ID. TFILE.
     001500 ENVIRONMENT DIVISION.
     001900 INPUT-OUTPUT SECTION.
     002000 FILE-CONTROL.
     002100     SELECT NEWFILE
     002110            ASSIGN TO "NFILE"
     002120            ORGANIZATION IS INDEXED
     002130            ACCESS MODE IS SEQUENTIAL
     002140            RECORD KEY IS NKEY WITH DUPLICATES.
     002200     SELECT TFILE ASSIGN TO OFILE.
     002300 DATA DIVISION.
     002400 FILE SECTION.
     002500 FD NEWFILE.
     002600 01 NREC.
     002610    05 NKEY PIC X(6).
     002620    05   PIC X(74).
     002700 FD TFILE.
     002800 01 TREC.
     002810    05 TKEY PIC X(6).
     002820    05   PIC X(74).
     003200 PROCEDURE DIVISION.
     003300 P1.
     003400      OPEN INPUT TFILE OUTPUT NEWFILE
     003410      READ TFILE AT END MOVE ALL "9" TO TREC END-READ
     003500      PERFORM UNTIL TREC = ALL "9"
     003600          WRITE NREC    FROM TREC
     003610               INVALID KEY DISPLAY "ERROR" TREC
     003620               END-WRITE
     003630          READ TFILE AT END MOVE ALL "9" TO TREC END-READ
     003700      END-PERFORM
     003710      CLOSE TFILE NEWFILE
     003780      STOP RUN.

Sequential Access of Indexed Files.   

For an indexed file, sequential access is in ascending key order.
Attempting to write a record out of order causes an invalid key error at
run time (file status code 21).

A sequential rewrite to an indexed file updates the record that was read
immediately before the REWRITE statement executed.  If the REWRITE
statement was not immediately preceded by a READ statement, a logic error
occurs.

The following table gives the modes in which you must open an indexed
file to perform the operations READ, WRITE, REWRITE, and DELETE. The KEY
IS phrase is not required.

          Table 5-5.  Modes to Open Indexed Files for Sequential Access 

---------------------------------------------------------------------------------------------
-                  Operation                  -         Modes in Which to Open File         -
---------------------------------------------------------------------------------------------
- READ                                        - INPUT or I-O                                -
---------------------------------------------------------------------------------------------
- WRITE                                       - OUTPUT, EXTEND, or I-O                      -
---------------------------------------------------------------------------------------------
- REWRITE                                     - I-O                                         -
---------------------------------------------------------------------------------------------
- DELETE                                      - I-O                                         -
---------------------------------------------------------------------------------------------

Random and Dynamic Access.   

For an indexed file, random and dynamic access use the primary key to
determine which record to WRITE, REWRITE, or DELETE.

Generic Keys.   

A generic key is a partial key, the first n digits of a key.  It is
specified in the START statement.

Example.   

In the following example, SOURCE-NO is a generic key.  The START
statement finds the first record that contains the generic key.

     SELECT I-FILE ASSIGN TO "INVFILE"
        ORGANIZATION IS INDEXED
        RECORD KEY IS PART-NO.
     01 IREC.
        05 PART-NO.
           10 SOURCE-NO         PIC XXXX.
           10 ITEM-NO           PIC XXXX.
        05 FILLER               PIC X(72).

     START I-FILE KEY = SOURCE-NO
        INVALID KEY DISPLAY "RECORD NOT FOUND"
        END-START.

Duplicate Keys.   

An indexed file can have duplicate primary keys if the WITH DUPLICATES
phrase is specified.  It can be specified for both the RECORD KEY and
ALTERNATE KEY clauses.  (Allowing the WITH DUPLICATES phrase with the
RECORD KEY clause is an HP extension.)

Duplicate primary keys severely limit the functions that can be performed
on the file.  In random or dynamic access, DELETE and REWRITE statements
apply only to the first record that has the specified primary key.



MPE/iX 5.0 Documentation