Creating Mailing Labels in COBOL

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUT-FILE
ASSIGN TO 'PERSON.DAT'.
SELECT PRINT-FILE
ASSIGN TO 'LABELS.TXT'.

DATA DIVISION.
FILE SECTION.

FD INPUT-FILE
DATA RECORD IS INPUT-RECORD.

01 INPUT-RECORD.
02 INPUT-ID PICTURE X(10).
02 INPUT-NAME PICTURE X(25).
02 INPUT-ADDRESS PICTURE X(25).
02 INPUT-CITY PICTURE X(15).
02 INPUT-STATE PICTURE X(2).
02 INPUT-ZIP PICTURE X(5).

FD PRINT-FILE
DATA RECORD IS PRINT-LINE.

01 PRINT-LINE PICTURE X(80).

WORKING-STORAGE SECTION.

01 LABEL-TABLE.
02 LABEL-ROW OCCURS 3 TIMES
INDEXED BY ROW-INDEX.
03 LABEL-COLUMN OCCURS 3 TIMES
INDEXED BY COLUMN-INDEX.
04 FILLER PICTURE X(1).
04 LABEL-FIELD PICTURE X(25).

01 NAME-FLIP-AREA.
02 FIRST-NAME PICTURE X(25).
02 LAST-NAME PICTURE X(25).
02 WHOLE-NAME PICTURE X(25).

01 WORKING-TABLE.
05 WORK-LETTER OCCURS 30 TIMES PICTURE X.

01 X PICTURE 9(2) COMP.

01 CONVERT-CASE-TEXT.
02 FILLER PICTURE X(52) VALUE
'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'.
01 CONVERT-CASE-TABLE REDEFINES CONVERT-CASE-TEXT.
02 CONVERT-CASE-LETTER OCCURS 26 TIMES
INDEXED BY CONVERT-INDEX.
03 CONVERT-UPPER PICTURE X.
03 CONVERT-LOWER PICTURE X.

01 CONVERT-FLAG PICTURE X.

01 EOF-FLAG PICTURE X.
88 END-OF-INPUT-FILE VALUE 'Y'.

PROCEDURE DIVISION.
MAIN-LINE.
PERFORM INITIALIZATION-ROUTINE.
PERFORM PRINT-LABELS
UNTIL END-OF-INPUT-FILE.
PERFORM EOJ-ROUTINE.
STOP RUN.

INITIALIZATION-ROUTINE.
OPEN INPUT INPUT-FILE.
OPEN OUTPUT PRINT-FILE.
MOVE 'N' TO EOF-FLAG.
READ INPUT-FILE
AT END MOVE 'Y' TO EOF-FLAG.

PRINT-LABELS.
MOVE SPACES TO LABEL-TABLE.
PERFORM BUILD-A-LABEL
VARYING COLUMN-INDEX FROM 1 BY 1
UNTIL COLUMN-INDEX > 3
OR END-OF-INPUT-FILE.
WRITE PRINT-LINE FROM LABEL-ROW (1).
WRITE PRINT-LINE FROM LABEL-ROW (2).
WRITE PRINT-LINE FROM LABEL-ROW (3).
MOVE SPACES TO PRINT-LINE.
WRITE PRINT-LINE AFTER ADVANCING 2 LINES.

BUILD-A-LABEL.
SET ROW-INDEX TO 1.
PERFORM NAME-FLIP-FLOP.
MOVE WHOLE-NAME TO WORKING-TABLE.
PERFORM CHANGE-TO-MIXED-CASE
VARYING X FROM 2 BY 1
UNTIL X > 25.
MOVE WORKING-TABLE TO LABEL-FIELD (ROW-INDEX, COLUMN-INDEX).
SET ROW-INDEX UP BY 1.
MOVE INPUT-ADDRESS TO WORKING-TABLE.
PERFORM CHANGE-TO-MIXED-CASE
VARYING X FROM 2 BY 1
UNTIL X > 25.
MOVE WORKING-TABLE TO LABEL-FIELD (ROW-INDEX, COLUMN-INDEX).
SET ROW-INDEX UP BY 1.
MOVE INPUT-CITY TO WORKING-TABLE.
PERFORM CHANGE-TO-MIXED-CASE
VARYING X FROM 2 BY 1
UNTIL X > 25.
STRING WORKING-TABLE DELIMITED BY ' '
', ' DELIMITED BY SIZE
INPUT-STATE DELIMITED BY SIZE
' ' DELIMITED BY SIZE
INPUT-ZIP DELIMITED BY SIZE
INTO LABEL-FIELD (ROW-INDEX, COLUMN-INDEX).
READ INPUT-FILE
AT END MOVE 'Y' TO EOF-FLAG.

NAME-FLIP-FLOP.
MOVE 1 TO X.
MOVE SPACES TO NAME-FLIP-AREA.
UNSTRING INPUT-NAME DELIMITED BY ', '
INTO LAST-NAME FIRST-NAME.
INSPECT LAST-NAME
REPLACING ALL ',' BY SPACE.
STRING FIRST-NAME DELIMITED BY ' '
' ' DELIMITED BY SIZE
LAST-NAME DELIMITED BY ' '
INTO WHOLE-NAME.

CHANGE-TO-MIXED-CASE.
* Start with the assumption that we will convert this letter to
* lower case (set flag to Yes) then look for exceptions that
* should be left upper case (set flag to No).
MOVE 'Y' TO CONVERT-FLAG.
IF (WORK-LETTER (X - 1) = " ")
OR (WORK-LETTER (X - 1) = "-")
OR (WORK-LETTER (X - 1) = "'")
OR (WORK-LETTER (X - 1) = ".")
MOVE 'N' TO CONVERT-FLAG.
IF (WORK-LETTER (X - 2) = "M"
AND WORK-LETTER (X - 1) = "c")
MOVE 'N' TO CONVERT-FLAG.
IF CONVERT-FLAG = 'Y'
PERFORM CONVERT-TO-LOWER-CASE.

CONVERT-TO-LOWER-CASE.
SET CONVERT-INDEX TO 1.
SEARCH CONVERT-CASE-LETTER
WHEN WORK-LETTER (X) = CONVERT-UPPER (CONVERT-INDEX)
MOVE CONVERT-LOWER (CONVERT-INDEX) TO WORK-LETTER (X).

EOJ-ROUTINE.
CLOSE INPUT-FILE.
CLOSE PRINT-FILE.

Sorting on Calculated Field in cobol

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\UNSORTC.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT OUT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\SORTC.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT SORT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\WORK.TMP'.

DATA DIVISION.
FILE SECTION.
FD IN-FILE
DATA RECORD IS IN-RECORD.
01 IN-RECORD.
02 IN-STUDENT-NAME PIC X(15).
02 IN-CREDITS-TAKEN PIC 9(3).
02 IN-QUALITY-POINTS PIC 9(3).

FD OUT-FILE
DATA RECORD IS OUT-RECORD.
01 OUT-RECORD PIC X(80).

SD SORT-FILE
DATA RECORD IS SORT-RECORD.
01 SORT-RECORD.
02 SORT-STUDENT-NAME PIC X(15).
02 SORT-CREDITS-TAKEN PIC 9(3).
02 SORT-QUALITY-POINTS PIC 9(3).
02 SORT-GPA PIC 9V99.

WORKING-STORAGE SECTION.

01 DETAIL-LINE.
02 DETAIL-STUDENT-NAME PIC X(15).
02 PIC X(3) VALUE SPACES.
02 DETAIL-GPA PIC 9.99.

01 SORTED-DATA-SWITCH PIC X VALUE 'N'.
88 NO-MORE-SORTED-DATA VALUE 'Y'.

01 EOF-SWITCH PIC X VALUE 'N'.
88 NO-MORE-INPUT-RECORDS VALUE 'Y'.

PROCEDURE DIVISION.
MAIN-LOGIC.
SORT SORT-FILE
ON DESCENDING KEY SORT-GPA
INPUT PROCEDURE IS CALCULATE-GPA
OUTPUT PROCEDURE IS PRINT-THE-REPORT.
STOP RUN.

CALCULATE-GPA.
OPEN INPUT IN-FILE.
READ IN-FILE
AT END MOVE 'Y' TO EOF-SWITCH.
PERFORM DATA-LOOP
UNTIL NO-MORE-INPUT-RECORDS.
CLOSE IN-FILE.

DATA-LOOP.
MOVE IN-STUDENT-NAME TO SORT-STUDENT-NAME.
MOVE IN-CREDITS-TAKEN TO SORT-CREDITS-TAKEN.
MOVE IN-QUALITY-POINTS TO SORT-QUALITY-POINTS.
COMPUTE SORT-GPA ROUNDED =
IN-QUALITY-POINTS / IN-CREDITS-TAKEN.
RELEASE SORT-RECORD.
READ IN-FILE
AT END MOVE 'Y' TO EOF-SWITCH.

PRINT-THE-REPORT.
OPEN OUTPUT OUT-FILE.
RETURN SORT-FILE
AT END MOVE 'Y' TO SORTED-DATA-SWITCH.
PERFORM REPORT-LOOP
UNTIL NO-MORE-SORTED-DATA.
CLOSE OUT-FILE.

REPORT-LOOP.
MOVE SORT-STUDENT-NAME TO DETAIL-STUDENT-NAME.
MOVE SORT-GPA TO DETAIL-GPA.
WRITE OUT-RECORD FROM DETAIL-LINE.
RETURN SORT-FILE
AT END MOVE 'Y' TO SORTED-DATA-SWITCH.

Input

CHARLIE HACKLEY010040
GORDON HUME 006018
TOMMY BEIDLER 012048
KATHY PECK 040080
MARY UECKER 020070
J.C. SANFORD 006000
DUANE NORTON 015042

Output (report sorted by calculated field G.P.A.)

CHARLIE HACKLEY 4.00
TOMMY BEIDLER 4.00
MARY UECKER 3.50
GORDON HUME 3.00
DUANE NORTON 2.80
KATHY PECK 2.00
J.C. SANFORD 0.00

Sorting Example: With Control Break in COBOL

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\UNSORTED.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT OUT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\SORTED4.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT SORT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\WORK.TMP'.

DATA DIVISION.
FILE SECTION.
FD IN-FILE
DATA RECORD IS IN-RECORD.
01 IN-RECORD.
02 IN-ID PIC 9(5).
02 IN-LAST-NAME PIC X(15).
02 IN-FIRST-NAME PIC X(10).
02 IN-CITY PIC X(12).
02 IN-STATE PIC X(2).
88 VALID-STATE-CODE VALUES 'AK'
'AL' 'AR' 'AZ' 'CA' 'CO' 'CT' 'DC' 'DE' 'FL' 'GA'
'HI' 'IA' 'ID' 'IL' 'IN' 'KS' 'KY' 'LA' 'MA' 'MD'
'ME' 'MI' 'MN' 'MO' 'MS' 'MT' 'NC' 'ND' 'NE' 'NH'
'NJ' 'NM' 'NV' 'NY' 'OH' 'OK' 'OR' 'PA' 'RI' 'SC'
'SD' 'TN' 'TX' 'UT' 'VA' 'VT' 'WA' 'WI' 'WV' 'WY'.

FD OUT-FILE
DATA RECORD IS OUT-RECORD.
01 OUT-RECORD PIC X(80).

SD SORT-FILE
DATA RECORD IS SORT-RECORD.
01 SORT-RECORD.
02 SORT-ID PIC 9(5).
02 SORT-LAST-NAME PIC X(15).
02 SORT-FIRST-NAME PIC X(10).
02 SORT-CITY PIC X(12).
02 SORT-STATE PIC X(2).

WORKING-STORAGE SECTION.

01 DETAIL-LINE.
02 DETAIL-ID PIC 9(5).
02 PIC X(3) VALUE SPACES.
02 DETAIL-LAST-NAME PIC X(15).
02 PIC X(3) VALUE SPACES.
02 DETAIL-FIRST-NAME PIC X(10).
02 PIC X(3) VALUE SPACES.
02 DETAIL-CITY PIC X(12).
02 PIC X(3) VALUE SPACES.
02 DETAIL-STATE PIC X(2).

01 SORTED-DATA-SWITCH PIC X VALUE 'N'.
88 NO-MORE-SORTED-DATA VALUE 'Y'.

01 EOF-SWITCH PIC X VALUE 'N'.
88 NO-MORE-INPUT-RECORDS VALUE 'Y'.

01 STATE-COUNT PIC 9(3) VALUE ZERO.

01 BREAK-LINE.
02 PIC X(8) VALUE SPACES.
02 PRINT-COUNT PIC ZZ9.
02 PIC X(12)
VALUE ' RECORDS IN '.
02 CURRENT-STATE PIC X(2).

PROCEDURE DIVISION.
MAIN-LOGIC.
SORT SORT-FILE
ON ASCENDING KEY SORT-STATE
ON ASCENDING KEY SORT-LAST-NAME
INPUT PROCEDURE IS INPUT-LOGIC
OUTPUT PROCEDURE IS CREATE-REPORT.
STOP RUN.

INPUT-LOGIC.
OPEN INPUT IN-FILE.
READ IN-FILE
AT END MOVE 'Y' TO EOF-SWITCH.
PERFORM DATA-LOOP
UNTIL NO-MORE-INPUT-RECORDS.
CLOSE IN-FILE.

DATA-LOOP.
IF VALID-STATE-CODE
MOVE IN-RECORD TO SORT-RECORD
RELEASE SORT-RECORD.
READ IN-FILE
AT END MOVE 'Y' TO EOF-SWITCH.

CREATE-REPORT.
OPEN OUTPUT OUT-FILE.
RETURN SORT-FILE
AT END MOVE 'Y' TO SORTED-DATA-SWITCH.
MOVE SORT-STATE TO CURRENT-STATE.
PERFORM REPORT-LOOP
UNTIL NO-MORE-SORTED-DATA.
PERFORM DO-CONTROL-BREAK.
CLOSE OUT-FILE.

REPORT-LOOP.
IF SORT-STATE NOT = CURRENT-STATE
PERFORM DO-CONTROL-BREAK.
MOVE SORT-ID TO DETAIL-ID
MOVE SORT-LAST-NAME TO DETAIL-LAST-NAME.
MOVE SORT-FIRST-NAME TO DETAIL-FIRST-NAME.
MOVE SORT-CITY TO DETAIL-CITY.
MOVE SORT-STATE TO DETAIL-STATE.
WRITE OUT-RECORD FROM DETAIL-LINE.
ADD 1 TO STATE-COUNT.
MOVE SORT-STATE TO CURRENT-STATE.
RETURN SORT-FILE
AT END MOVE 'Y' TO SORTED-DATA-SWITCH.

DO-CONTROL-BREAK.
MOVE STATE-COUNT TO PRINT-COUNT.
WRITE OUT-RECORD FROM BREAK-LINE AFTER 2.
MOVE ZERO TO STATE-COUNT
MOVE SPACES TO OUT-RECORD.
WRITE OUT-RECORD.

Input

91163Dube Jason Chicago IL
61535King Adriane Cincinnati OH
45636Holtz Rachel Newport KY
10216Gehl Bryan Louisville KY
52158Flynn Ashley Muskegon MI
22277Theis Hanah Norwood OH
38551Solpa Mac Lansing MI
78496McCoy Amanda Brookfield WI
65720Read Sean Springfield BF
94000Waymel Bradley Kalamazoo MI
42966Vilella Reza Urbana IL
04928Streeter Farhad Toledo OH
69216Steele Karen Covington KY
56913Moreno Jan Dayton OH
34932Vogel Sandra Grand RapidsMI

Output (report with control break)

91163 Dube Jason Chicago IL
42966 Vilella Reza Urbana IL

2 RECORDS IN IL

10216 Gehl Bryan Louisville KY
45636 Holtz Rachel Newport KY
69216 Steele Karen Covington KY

3 RECORDS IN KY

52158 Flynn Ashley Muskegon MI
38551 Solpa Mac Lansing MI
34932 Vogel Sandra Grand Rapids MI
94000 Waymel Bradley Kalamazoo MI

4 RECORDS IN MI

61535 King Adriane Cincinnati OH
56913 Moreno Jan Dayton OH
04928 Streeter Farhad Toledo OH
22277 Theis Hanah Norwood OH

4 RECORDS IN OH

78496 McCoy Amanda Brookfield WI

1 RECORDS IN WI

Sort Example: With Header and Footer

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\UNSORTED.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT OUT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\SORTED3A.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT SORT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\WORK.TMP'.

DATA DIVISION.
FILE SECTION.
FD IN-FILE
DATA RECORD IS IN-RECORD.
01 IN-RECORD.
02 IN-ID PIC 9(5).
02 IN-LAST-NAME PIC X(15).
02 IN-FIRST-NAME PIC X(10).
02 IN-CITY PIC X(12).
02 IN-STATE PIC X(2).
88 VALID-STATE-CODE VALUES 'AK'
'AL' 'AR' 'AZ' 'CA' 'CO' 'CT' 'DC' 'DE' 'FL' 'GA'
'HI' 'IA' 'ID' 'IL' 'IN' 'KS' 'KY' 'LA' 'MA' 'MD'
'ME' 'MI' 'MN' 'MO' 'MS' 'MT' 'NC' 'ND' 'NE' 'NH'
'NJ' 'NM' 'NV' 'NY' 'OH' 'OK' 'OR' 'PA' 'RI' 'SC'
'SD' 'TN' 'TX' 'UT' 'VA' 'VT' 'WA' 'WI' 'WV' 'WY'.

FD OUT-FILE
DATA RECORD IS OUT-RECORD.
01 OUT-RECORD PIC X(80).

SD SORT-FILE
DATA RECORD IS SORT-RECORD.
01 SORT-RECORD.
02 SORT-ID PIC 9(5).
02 SORT-LAST-NAME PIC X(15).
02 SORT-FIRST-NAME PIC X(10).
02 SORT-CITY PIC X(12).
02 SORT-STATE PIC X(2).

WORKING-STORAGE SECTION.

01 DETAIL-LINE.
02 DETAIL-ID PIC 9(5).
02 PIC X(3) VALUE SPACES.
02 DETAIL-LAST-NAME PIC X(15).
02 PIC X(3) VALUE SPACES.
02 DETAIL-FIRST-NAME PIC X(10).
02 PIC X(3) VALUE SPACES.
02 DETAIL-CITY PIC X(12).
02 PIC X(3) VALUE SPACES.
02 DETAIL-STATE PIC X(2).

01 SORTED-DATA-SWITCH PIC X VALUE 'N'.
88 NO-MORE-SORTED-DATA VALUE 'Y'.

01 EOF-SWITCH PIC X VALUE 'N'.
88 NO-MORE-INPUT-RECORDS VALUE 'Y'.

01 RECORD-COUNT PIC 9(3) VALUE ZERO.

01 HEADER-LINE.
02 PIC X(8) VALUE 'ID NUM'.
02 PIC X(18) VALUE 'LAST NAME'.
02 PIC X(13) VALUE 'FIRST NAME'.
02 PIC X(15) VALUE 'CITY'.
02 PIC X(5) VALUE 'STATE'.

01 FOOTER-LINE.
02 PIC X(8) VALUE SPACES.
02 FOOTER-COUNT PIC ZZ9.
02 PIC X(20)
VALUE ' RECORDS PROCESSED'.

PROCEDURE DIVISION.
MAIN-LOGIC.
SORT SORT-FILE
ON ASCENDING KEY SORT-LAST-NAME
INPUT PROCEDURE IS INPUT-LOGIC
OUTPUT PROCEDURE IS CREATE-REPORT.
STOP RUN.

INPUT-LOGIC.
OPEN INPUT IN-FILE.
READ IN-FILE
AT END MOVE 'Y' TO EOF-SWITCH.
PERFORM DATA-LOOP
UNTIL NO-MORE-INPUT-RECORDS.
CLOSE IN-FILE.

DATA-LOOP.
IF VALID-STATE-CODE
MOVE IN-RECORD TO SORT-RECORD
RELEASE SORT-RECORD.
READ IN-FILE
AT END MOVE 'Y' TO EOF-SWITCH.

CREATE-REPORT.
OPEN OUTPUT OUT-FILE.
PERFORM PRINT-HEADERS.
RETURN SORT-FILE
AT END MOVE 'Y' TO SORTED-DATA-SWITCH.
PERFORM REPORT-LOOP
UNTIL NO-MORE-SORTED-DATA.
PERFORM PRINT-FOOTERS.
CLOSE OUT-FILE.

REPORT-LOOP.
MOVE SORT-ID TO DETAIL-ID
MOVE SORT-LAST-NAME TO DETAIL-LAST-NAME.
MOVE SORT-FIRST-NAME TO DETAIL-FIRST-NAME.
MOVE SORT-CITY TO DETAIL-CITY.
MOVE SORT-STATE TO DETAIL-STATE.
WRITE OUT-RECORD FROM DETAIL-LINE.
ADD 1 TO RECORD-COUNT.
RETURN SORT-FILE
AT END MOVE 'Y' TO SORTED-DATA-SWITCH.

PRINT-HEADERS.
WRITE OUT-RECORD FROM HEADER-LINE.
MOVE SPACES TO OUT-RECORD.
WRITE OUT-RECORD.

PRINT-FOOTERS.
MOVE RECORD-COUNT TO FOOTER-COUNT.
WRITE OUT-RECORD FROM FOOTER-LINE AFTER 2.

Input

91163Dube Jason Chicago IL
61535King Adriane Cincinnati OH
45636Holtz Rachel Newport KY
10216Gehl Bryan Louisville KY
52158Flynn Ashley Muskegon MI
22277Theis Hanah Norwood OH
38551Solpa Mac Lansing MI
78496McCoy Amanda Brookfield WI
65720Read Sean Springfield BF
94000Waymel Bradley Kalamazoo MI
42966Vilella Reza Urbana IL
04928Streeter Farhad Toledo OH
69216Steele Karen Covington KY
56913Moreno Jan Dayton OH
34932Vogel Sandra Grand RapidsMI

Output

ID NUM LAST NAME FIRST NAME CITY STATE

91163 Dube Jason Chicago IL
52158 Flynn Ashley Muskegon MI
10216 Gehl Bryan Louisville KY
45636 Holtz Rachel Newport KY
61535 King Adriane Cincinnati OH
78496 McCoy Amanda Brookfield WI
56913 Moreno Jan Dayton OH
38551 Solpa Mac Lansing MI
69216 Steele Karen Covington KY
04928 Streeter Farhad Toledo OH
22277 Theis Hanah Norwood OH
42966 Vilella Reza Urbana IL
34932 Vogel Sandra Grand Rapids MI
94000 Waymel Bradley Kalamazoo MI

14 RECORDS PROCESSED

Sort Example: OUTPUT PROCEDURE In Cobol

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\UNSORTED.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT OUT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\SORTED3.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT SORT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\WORK.TMP'.

DATA DIVISION.
FILE SECTION.
FD IN-FILE
DATA RECORD IS IN-RECORD.
01 IN-RECORD.
02 IN-ID PIC 9(5).
02 IN-LAST-NAME PIC X(15).
02 IN-FIRST-NAME PIC X(10).
02 IN-CITY PIC X(12).
02 IN-STATE PIC X(2).
88 VALID-STATE-CODE VALUES 'AK'
'AL' 'AR' 'AZ' 'CA' 'CO' 'CT' 'DC' 'DE' 'FL' 'GA'
'HI' 'IA' 'ID' 'IL' 'IN' 'KS' 'KY' 'LA' 'MA' 'MD'
'ME' 'MI' 'MN' 'MO' 'MS' 'MT' 'NC' 'ND' 'NE' 'NH'
'NJ' 'NM' 'NV' 'NY' 'OH' 'OK' 'OR' 'PA' 'RI' 'SC'
'SD' 'TN' 'TX' 'UT' 'VA' 'VT' 'WA' 'WI' 'WV' 'WY'.

FD OUT-FILE
DATA RECORD IS OUT-RECORD.
01 OUT-RECORD PIC X(80).

SD SORT-FILE
DATA RECORD IS SORT-RECORD.
01 SORT-RECORD.
02 SORT-ID PIC 9(5).
02 SORT-LAST-NAME PIC X(15).
02 SORT-FIRST-NAME PIC X(10).
02 SORT-CITY PIC X(12).
02 SORT-STATE PIC X(2).

WORKING-STORAGE SECTION.

01 DETAIL-LINE.
02 DETAIL-ID PIC 9(5).
02 PIC X(3) VALUE SPACES.
02 DETAIL-LAST-NAME PIC X(15).
02 PIC X(3) VALUE SPACES.
02 DETAIL-FIRST-NAME PIC X(10).
02 PIC X(3) VALUE SPACES.
02 DETAIL-CITY PIC X(12).
02 PIC X(3) VALUE SPACES.
02 DETAIL-STATE PIC X(2).

01 SORTED-DATA-SWITCH PIC X VALUE 'N'.
88 NO-MORE-SORTED-DATA VALUE 'Y'.

01 EOF-SWITCH PIC X VALUE 'N'.
88 NO-MORE-INPUT-RECORDS VALUE 'Y'.

PROCEDURE DIVISION.
MAIN-LOGIC.
SORT SORT-FILE
ON ASCENDING KEY SORT-LAST-NAME
INPUT PROCEDURE IS INPUT-LOGIC
OUTPUT PROCEDURE IS CREATE-REPORT.
STOP RUN.

INPUT-LOGIC.
OPEN INPUT IN-FILE.
READ IN-FILE
AT END MOVE 'Y' TO EOF-SWITCH.
PERFORM DATA-LOOP
UNTIL NO-MORE-INPUT-RECORDS.
CLOSE IN-FILE.

DATA-LOOP.
IF VALID-STATE-CODE
MOVE IN-RECORD TO SORT-RECORD
RELEASE SORT-RECORD.
READ IN-FILE
AT END MOVE 'Y' TO EOF-SWITCH.

CREATE-REPORT.
OPEN OUTPUT OUT-FILE.
RETURN SORT-FILE
AT END MOVE 'Y' TO SORTED-DATA-SWITCH.
PERFORM REPORT-LOOP
UNTIL NO-MORE-SORTED-DATA.
CLOSE OUT-FILE.

REPORT-LOOP.
MOVE SORT-ID TO DETAIL-ID
MOVE SORT-LAST-NAME TO DETAIL-LAST-NAME.
MOVE SORT-FIRST-NAME TO DETAIL-FIRST-NAME.
MOVE SORT-CITY TO DETAIL-CITY.
MOVE SORT-STATE TO DETAIL-STATE.
WRITE OUT-RECORD FROM DETAIL-LINE.
RETURN SORT-FILE
AT END MOVE 'Y' TO SORTED-DATA-SWITCH.

Input

91163Dube Jason Chicago IL
61535King Adriane Cincinnati OH
45636Holtz Rachel Newport KY
10216Gehl Bryan Louisville KY
52158Flynn Ashley Muskegon MI
22277Theis Hanah Norwood OH
38551Solpa Mac Lansing MI
78496McCoy Amanda Brookfield WI
65720Read Sean Springfield BF
94000Waymel Bradley Kalamazoo MI
42966Vilella Reza Urbana IL
04928Streeter Farhad Toledo OH
69216Steele Karen Covington KY
56913Moreno Jan Dayton OH
34932Vogel Sandra Grand RapidsMI

Output (report sorted by name)

91163 Dube Jason Chicago IL
52158 Flynn Ashley Muskegon MI
10216 Gehl Bryan Louisville KY
45636 Holtz Rachel Newport KY
61535 King Adriane Cincinnati OH
78496 McCoy Amanda Brookfield WI
56913 Moreno Jan Dayton OH
38551 Solpa Mac Lansing MI
69216 Steele Karen Covington KY
04928 Streeter Farhad Toledo OH
22277 Theis Hanah Norwood OH
42966 Vilella Reza Urbana IL
34932 Vogel Sandra Grand Rapids MI
94000 Waymel Bradley Kalamazoo MI

INPUT PROCEDURE with inline PERFORM

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\UNSORTED.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT OUT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\SORTED2A.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT SORT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\WORK.TMP'.

DATA DIVISION.
FILE SECTION.
FD IN-FILE
DATA RECORD IS IN-RECORD.
01 IN-RECORD.
02 IN-ID PIC 9(5).
02 IN-LAST-NAME PIC X(15).
02 IN-FIRST-NAME PIC X(10).
02 IN-CITY PIC X(12).
02 IN-STATE PIC X(2).
88 VALID-STATE-CODE VALUES 'AK'
'AL' 'AR' 'AZ' 'CA' 'CO' 'CT' 'DC' 'DE' 'FL' 'GA'
'HI' 'IA' 'ID' 'IL' 'IN' 'KS' 'KY' 'LA' 'MA' 'MD'
'ME' 'MI' 'MN' 'MO' 'MS' 'MT' 'NC' 'ND' 'NE' 'NH'
'NJ' 'NM' 'NV' 'NY' 'OH' 'OK' 'OR' 'PA' 'RI' 'SC'
'SD' 'TN' 'TX' 'UT' 'VA' 'VT' 'WA' 'WI' 'WV' 'WY'.

FD OUT-FILE
DATA RECORD IS OUT-RECORD.
01 OUT-RECORD.
02 OUT-ID PIC 9(5).
02 OUT-LAST-NAME PIC X(15).
02 OUT-FIRST-NAME PIC X(10).
02 OUT-CITY PIC X(12).
02 OUT-STATE PIC X(2).

SD SORT-FILE
DATA RECORD IS SORT-RECORD.
01 SORT-RECORD.
02 SORT-ID PIC 9(5).
02 SORT-LAST-NAME PIC X(15).
02 SORT-FIRST-NAME PIC X(10).
02 SORT-CITY PIC X(12).
02 SORT-STATE PIC X(2).

WORKING-STORAGE SECTION.

01 EOF-SWITCH PIC X VALUE 'N'.
88 NO-MORE-INPUT-RECORDS VALUE 'Y'.

PROCEDURE DIVISION.
MAIN-LOGIC.
SORT SORT-FILE
ON ASCENDING KEY SORT-LAST-NAME
INPUT PROCEDURE IS INPUT-LOGIC
GIVING OUT-FILE.
STOP RUN.

INPUT-LOGIC.
OPEN INPUT IN-FILE.
PERFORM UNTIL NO-MORE-INPUT-RECORDS
READ IN-FILE
AT END
MOVE 'Y' TO EOF-SWITCH
NOT AT END
IF VALID-STATE-CODE
MOVE IN-RECORD TO SORT-RECORD
RELEASE SORT-RECORD
END-IF
END-READ
END-PERFORM.
CLOSE IN-FILE.

Input

91163Dube Jason Chicago IL
61535King Adriane Cincinnati OH
45636Holtz Rachel Newport KY
10216Gehl Bryan Louisville KY
52158Flynn Ashley Muskegon MI
22277Theis Hanah Norwood OH
38551Solpa Mac Lansing MI
78496McCoy Amanda Brookfield WI
65720Read Sean Springfield BF
94000Waymel Bradley Kalamazoo MI
42966Vilella Reza Urbana IL
04928Streeter Farhad Toledo OH
69216Steele Karen Covington KY
56913Moreno Jan Dayton OH
34932Vogel Sandra Grand RapidsMI

Output (sorted by name with invalid state code omitted)


91163Dube Jason Chicago IL
52158Flynn Ashley Muskegon MI
10216Gehl Bryan Louisville KY
45636Holtz Rachel Newport KY
61535King Adriane Cincinnati OH
78496McCoy Amanda Brookfield WI
56913Moreno Jan Dayton OH
38551Solpa Mac Lansing MI
69216Steele Karen Covington KY
04928Streeter Farhad Toledo OH
22277Theis Hanah Norwood OH
42966Vilella Reza Urbana IL
34932Vogel Sandra Grand RapidsMI
94000Waymel Bradley Kalamazoo MI

Sort Example: INPUT PROCEDURE

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\UNSORTED.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT OUT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\SORTED2.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT SORT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\WORK.TMP'.

DATA DIVISION.
FILE SECTION.
FD IN-FILE
DATA RECORD IS IN-RECORD.
01 IN-RECORD.
02 IN-ID PIC 9(5).
02 IN-LAST-NAME PIC X(15).
02 IN-FIRST-NAME PIC X(10).
02 IN-CITY PIC X(12).
02 IN-STATE PIC X(2).
88 VALID-STATE-CODE VALUES 'AK'
'AL' 'AR' 'AZ' 'CA' 'CO' 'CT' 'DC' 'DE' 'FL' 'GA'
'HI' 'IA' 'ID' 'IL' 'IN' 'KS' 'KY' 'LA' 'MA' 'MD'
'ME' 'MI' 'MN' 'MO' 'MS' 'MT' 'NC' 'ND' 'NE' 'NH'
'NJ' 'NM' 'NV' 'NY' 'OH' 'OK' 'OR' 'PA' 'RI' 'SC'
'SD' 'TN' 'TX' 'UT' 'VA' 'VT' 'WA' 'WI' 'WV' 'WY'.

FD OUT-FILE
DATA RECORD IS OUT-RECORD.
01 OUT-RECORD.
02 OUT-ID PIC 9(5).
02 OUT-LAST-NAME PIC X(15).
02 OUT-FIRST-NAME PIC X(10).
02 OUT-CITY PIC X(12).
02 OUT-STATE PIC X(2).

SD SORT-FILE
DATA RECORD IS SORT-RECORD.
01 SORT-RECORD.
02 SORT-ID PIC 9(5).
02 SORT-LAST-NAME PIC X(15).
02 SORT-FIRST-NAME PIC X(10).
02 SORT-CITY PIC X(12).
02 SORT-STATE PIC X(2).

WORKING-STORAGE SECTION.

01 EOF-SWITCH PIC X VALUE 'N'.
88 NO-MORE-INPUT-RECORDS VALUE 'Y'.

PROCEDURE DIVISION.
MAIN-LOGIC.
SORT SORT-FILE
ON ASCENDING KEY SORT-LAST-NAME
INPUT PROCEDURE IS INPUT-LOGIC
GIVING OUT-FILE.
STOP RUN.

INPUT-LOGIC.
OPEN INPUT IN-FILE.
READ IN-FILE
AT END MOVE 'Y' TO EOF-SWITCH.
PERFORM DATA-LOOP
UNTIL NO-MORE-INPUT-RECORDS.
CLOSE IN-FILE.

DATA-LOOP.
IF VALID-STATE-CODE
MOVE IN-RECORD TO SORT-RECORD
RELEASE SORT-RECORD.
READ IN-FILE
AT END MOVE 'Y' TO EOF-SWITCH.

Input

91163Dube           Jason     Chicago     IL
61535King Adriane Cincinnati OH
45636Holtz Rachel Newport KY
10216Gehl Bryan Louisville KY
52158Flynn Ashley Muskegon MI
22277Theis Hanah Norwood OH
38551Solpa Mac Lansing MI
78496McCoy Amanda Brookfield WI
65720Read Sean Springfield BF
94000Waymel Bradley Kalamazoo MI
42966Vilella Reza Urbana IL
04928Streeter Farhad Toledo OH
69216Steele Karen Covington KY
56913Moreno Jan Dayton OH
34932Vogel Sandra Grand RapidsMI

Output (sorted by name with invalid state code omitted)

91163Dube           Jason     Chicago     IL
52158Flynn Ashley Muskegon MI
10216Gehl Bryan Louisville KY
45636Holtz Rachel Newport KY
61535King Adriane Cincinnati OH
78496McCoy Amanda Brookfield WI
56913Moreno Jan Dayton OH
38551Solpa Mac Lansing MI
69216Steele Karen Covington KY
04928Streeter Farhad Toledo OH
22277Theis Hanah Norwood OH
42966Vilella Reza Urbana IL
34932Vogel Sandra Grand RapidsMI
94000Waymel Bradley Kalamazoo MI

Multiple Sort Keys In COBOL

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\UNSORTED.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT OUT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\SORTED1A.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT SORT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\WORK.TMP'.

DATA DIVISION.
FILE SECTION.
FD IN-FILE
DATA RECORD IS IN-RECORD.
01 IN-RECORD.
02 IN-ID PIC 9(5).
02 IN-LAST-NAME PIC X(15).
02 IN-FIRST-NAME PIC X(10).
02 IN-CITY PIC X(12).
02 IN-STATE PIC X(2).

FD OUT-FILE
DATA RECORD IS OUT-RECORD.
01 OUT-RECORD.
02 OUT-ID PIC 9(5).
02 OUT-LAST-NAME PIC X(15).
02 OUT-FIRST-NAME PIC X(10).
02 OUT-CITY PIC X(12).
02 OUT-STATE PIC X(2).

SD SORT-FILE
DATA RECORD IS SORT-RECORD.
01 SORT-RECORD.
02 SORT-ID PIC 9(5).
02 SORT-LAST-NAME PIC X(15).
02 SORT-FIRST-NAME PIC X(10).
02 SORT-CITY PIC X(12).
02 SORT-STATE PIC X(2).

WORKING-STORAGE SECTION.

PROCEDURE DIVISION.
MAINLINE.
SORT SORT-FILE
ON ASCENDING KEY SORT-STATE
ON ASCENDING KEY SORT-LAST-NAME
USING IN-FILE
GIVING OUT-FILE.
STOP RUN.

Input

91163Dube           Jason     Chicago     IL
61535King Adriane Cincinnati OH
45636Holtz Rachel Newport KY
10216Gehl Bryan Louisville KY
52158Flynn Ashley Muskegon MI
22277Theis Hanah Norwood OH
38551Solpa Mac Lansing MI
78496McCoy Amanda Brookfield WI
65720Read Sean Springfield BF
94000Waymel Bradley Kalamazoo MI
42966Vilella Reza Urbana IL
04928Streeter Farhad Toledo OH
69216Steele Karen Covington KY
56913Moreno Jan Dayton OH
34932Vogel Sandra Grand RapidsMI

Output (sorted by state then by name)

65720Read           Sean      Springfield BF
91163Dube Jason Chicago IL
42966Vilella Reza Urbana IL
10216Gehl Bryan Louisville KY
45636Holtz Rachel Newport KY
69216Steele Karen Covington KY
52158Flynn Ashley Muskegon MI
38551Solpa Mac Lansing MI
34932Vogel Sandra Grand RapidsMI
94000Waymel Bradley Kalamazoo MI
61535King Adriane Cincinnati OH
56913Moreno Jan Dayton OH
04928Streeter Farhad Toledo OH
22277Theis Hanah Norwood OH
78496McCoy Amanda Brookfield WI

Program to show USING and GIVING options of the SORT.

 ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\UNSORTED.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT OUT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\SORTED1.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT SORT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\WORK.TMP'.

DATA DIVISION.
FILE SECTION.
FD IN-FILE
DATA RECORD IS IN-RECORD.
01 IN-RECORD.
02 IN-ID PIC 9(5).
02 IN-LAST-NAME PIC X(15).
02 IN-FIRST-NAME PIC X(10).
02 IN-CITY PIC X(12).
02 IN-STATE PIC X(2).

FD OUT-FILE
DATA RECORD IS OUT-RECORD.
01 OUT-RECORD.
02 OUT-ID PIC 9(5).
02 OUT-LAST-NAME PIC X(15).
02 OUT-FIRST-NAME PIC X(10).
02 OUT-CITY PIC X(12).
02 OUT-STATE PIC X(2).

SD SORT-FILE
DATA RECORD IS SORT-RECORD.
01 SORT-RECORD.
02 SORT-ID PIC 9(5).
02 SORT-LAST-NAME PIC X(15).
02 SORT-FIRST-NAME PIC X(10).
02 SORT-CITY PIC X(12).
02 SORT-STATE PIC X(2).

WORKING-STORAGE SECTION.

PROCEDURE DIVISION.
MAINLINE.
SORT SORT-FILE
ON ASCENDING KEY SORT-LAST-NAME
USING IN-FILE
GIVING OUT-FILE.
STOP RUN.

Input

91163Dube           Jason     Chicago     IL
61535King Adriane Cincinnati OH
45636Holtz Rachel Newport KY
10216Gehl Bryan Louisville KY
52158Flynn Ashley Muskegon MI
22277Theis Hanah Norwood OH
38551Solpa Mac Lansing MI
78496McCoy Amanda Brookfield WI
65720Read Sean Springfield BF
94000Waymel Bradley Kalamazoo MI
42966Vilella Reza Urbana IL
04928Streeter Farhad Toledo OH
69216Steele Karen Covington KY
56913Moreno Jan Dayton OH
34932Vogel Sandra Grand RapidsMI

Output (sorted by last name)

91163Dube           Jason     Chicago     IL
52158Flynn Ashley Muskegon MI
10216Gehl Bryan Louisville KY
45636Holtz Rachel Newport KY
61535King Adriane Cincinnati OH
78496McCoy Amanda Brookfield WI
56913Moreno Jan Dayton OH
65720Read Sean Springfield BF
38551Solpa Mac Lansing MI
69216Steele Karen Covington KY
04928Streeter Farhad Toledo OH
22277Theis Hanah Norwood OH
42966Vilella Reza Urbana IL
34932Vogel Sandra Grand RapidsMI
94000Waymel Bradley Kalamazoo MI

Use Excel to Compute COBOL Results

Does Excel belong in a COBOL class? We think it does because it is a fantastic tool with which to compute anticipated output, the first step in developing any COBOL program. The first thing I do as an instructor in assigning a project, is to require students to submit the anticipated output based on the sample data supplied with the program. They can do this via pencil and paper. They can also do it in Excel depending on how much of the application they remember. But it's easy. In essence you open a data file in Excel (be sure to specify all files as the file type) and the Input Wizard will convert the txt file to a workbook. From there you can use formulas as appropriate to get the output.

As indicated, the first step in the assignment is for the student to read the processing specifications and to compute the intended results. The typical solution is traditional, with pencil and paper, but you can facilitate the process with an Excel worksheet. Regardless, however, of how the student determines the anticipated output, computation of the output does several things:

  1. It confirms for both student and instructor the desired result of the COBOL program. The student knows the intended result and the instructor has an invaluable aid in grading the projects.
  2. It eliminates any ambiguities that might exist in the specifications and/or clarifies the intended result of more complicated specifications.
  3. It reinforces material learned in an earlier class; moreover the concept is applicable to programming in any language.

Once the class is introduced to Excel, the next question is inevitably "If I can do this in 10 minutes in Excel, why do I need two weeks to write a COBOL program?" The answer in a word is the I/O capability that is built into COBOL to process large files, which far exceeds the capability of a spreadsheet. Of course not every project is amenable to a solution by Excel, but you will be pleasantly surprised at how many fit. More advanced projects will require the Excel IF, VLOOKUP, and SUBTOTAL functions, but students enjoy this review.


Using the Primary Key for a Record Look-up

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PERSON-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\I\PERSON.DAT'
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS PERSON-KEY
ALTERNATE RECORD KEY IS PERSON-ALTERNATE-KEY
WITH DUPLICATES
FILE STATUS IS IO-FILE-STATUS.

DATA DIVISION.
FILE SECTION.

FD PERSON-FILE
DATA RECORD IS PERSON-ITEM.

01 PERSON-ITEM.
02 PERSON-KEY.
03 PERSON-ID PICTURE X(4).
02 PERSON-ALTERNATE-KEY.
03 PERSON-NAME PICTURE X(20).
02 PERSON-ADDRESS PICTURE X(20).
02 PERSON-CITY PICTURE X(18).
02 PERSON-STATE PICTURE X(2).
02 PERSON-ZIP PICTURE X(5).
02 PERSON-PHONE.
03 PERSON-AREA-CODE PICTURE X(3).
03 PERSON-PHONE-NUMBER PICTURE X(7).
02 PERSON-STATUS PICTURE X.

WORKING-STORAGE SECTION.

01 INVALID-PERSON-KEY-FLAG PICTURE X.
88 INVALID-PERSON-KEY VALUE 'Y'.
88 VALID-PERSON-KEY VALUE 'N'.

01 IO-FILE-STATUS PICTURE X(2).

PROCEDURE DIVISION.

MAIN-LINE.
PERFORM INITIALIZATION-ROUTINE.
PERFORM LOOKUP-A-RECORD.
PERFORM EOJ-ROUTINE.
STOP RUN.


INITIALIZATION-ROUTINE.
OPEN I-O PERSON-FILE.


LOOKUP-A-RECORD.
DISPLAY 'Enter ID Number for the record you want to view: '.
ACCEPT PERSON-KEY.
PERFORM GET-PERSON-ITEM.
IF INVALID-PERSON-KEY
DISPLAY 'Record not found'
ELSE
DISPLAY PERSON-NAME
DISPLAY PERSON-ADDRESS
DISPLAY PERSON-CITY ' ' PERSON-STATE ' ' PERSON-ZIP
DISPLAY '(' PERSON-AREA-CODE ') '
PERSON-PHONE-NUMBER (1:3) '-'
PERSON-PHONE-NUMBER (4:4)
END-IF.


EOJ-ROUTINE.
CLOSE PERSON-FILE.


GET-PERSON-ITEM.
MOVE 'N' TO INVALID-PERSON-KEY-FLAG.
READ PERSON-FILE
INVALID KEY
MOVE 'Y' TO INVALID-PERSON-KEY-FLAG.

Program to Delete a Record from an Indexed File

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PERSON-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\I\PERSON.DAT'
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS PERSON-KEY
ALTERNATE RECORD KEY IS PERSON-ALTERNATE-KEY
WITH DUPLICATES
FILE STATUS IS IO-FILE-STATUS.

DATA DIVISION.
FILE SECTION.

FD PERSON-FILE
DATA RECORD IS PERSON-ITEM.

01 PERSON-ITEM.
02 PERSON-KEY.
03 PERSON-ID PICTURE X(4).
02 PERSON-ALTERNATE-KEY.
03 PERSON-NAME PICTURE X(20).
02 PERSON-ADDRESS PICTURE X(20).
02 PERSON-CITY PICTURE X(18).
02 PERSON-STATE PICTURE X(2).
02 PERSON-ZIP PICTURE X(5).
02 PERSON-PHONE.
03 PERSON-AREA-CODE PICTURE X(3).
03 PERSON-PHONE-NUMBER PICTURE X(7).
02 PERSON-STATUS PICTURE X.

WORKING-STORAGE SECTION.

01 INVALID-PERSON-KEY-FLAG PICTURE X.
88 INVALID-PERSON-KEY VALUE 'Y'.
88 VALID-PERSON-KEY VALUE 'N'.

01 IO-FILE-STATUS PICTURE X(2).

PROCEDURE DIVISION.

MAIN-LINE.
PERFORM INITIALIZATION-ROUTINE.
PERFORM DELETE-A-RECORD.
PERFORM EOJ-ROUTINE.
STOP RUN.


INITIALIZATION-ROUTINE.
OPEN I-O PERSON-FILE.


DELETE-A-RECORD.
DISPLAY 'Enter ID Number for the record you want to delete: '.
ACCEPT PERSON-KEY.
PERFORM DELETE-PERSON-ITEM.
IF INVALID-PERSON-KEY
DISPLAY 'Record not found'
ELSE
DISPLAY 'Record deleted'
END-IF.


EOJ-ROUTINE.
CLOSE PERSON-FILE.


DELETE-PERSON-ITEM.
MOVE 'N' TO INVALID-PERSON-KEY-FLAG.
DELETE PERSON-FILE
INVALID KEY
MOVE 'Y' TO INVALID-PERSON-KEY-FLAG.

Updating a Record in an Indexed File

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PERSON-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\I\PERSON.DAT'
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS PERSON-KEY
ALTERNATE RECORD KEY IS PERSON-ALTERNATE-KEY
WITH DUPLICATES
FILE STATUS IS IO-FILE-STATUS.

DATA DIVISION.
FILE SECTION.

FD PERSON-FILE
DATA RECORD IS PERSON-ITEM.

01 PERSON-ITEM.
02 PERSON-KEY.
03 PERSON-ID PICTURE X(4).
02 PERSON-ALTERNATE-KEY.
03 PERSON-NAME PICTURE X(20).
02 PERSON-ADDRESS PICTURE X(20).
02 PERSON-CITY PICTURE X(18).
02 PERSON-STATE PICTURE X(2).
02 PERSON-ZIP PICTURE X(5).
02 PERSON-PHONE.
03 PERSON-AREA-CODE PICTURE X(3).
03 PERSON-PHONE-NUMBER PICTURE X(7).
02 PERSON-STATUS PICTURE X.

WORKING-STORAGE SECTION.

01 INVALID-PERSON-KEY-FLAG PICTURE X.
88 INVALID-PERSON-KEY VALUE 'Y'.
88 VALID-PERSON-KEY VALUE 'N'.

01 IO-FILE-STATUS PICTURE X(2).

PROCEDURE DIVISION.

MAIN-LINE.
PERFORM INITIALIZATION-ROUTINE.
PERFORM CHANGE-A-RECORD.
PERFORM EOJ-ROUTINE.
STOP RUN.


INITIALIZATION-ROUTINE.
OPEN I-O PERSON-FILE.


CHANGE-A-RECORD.
DISPLAY 'Enter ID Number for the record you want to change: '.
ACCEPT PERSON-KEY.
PERFORM GET-PERSON-ITEM.
IF INVALID-PERSON-KEY
DISPLAY 'Record not found'
ELSE
DISPLAY PERSON-NAME
DISPLAY PERSON-ADDRESS
DISPLAY PERSON-CITY ' ' PERSON-STATE ' ' PERSON-ZIP
DISPLAY 'Enter new area code and phone number: '
ACCEPT PERSON-PHONE
PERFORM REPLACE-PERSON-ITEM
IF VALID-PERSON-KEY
DISPLAY 'Phone number updated: '
'(' PERSON-AREA-CODE ') '
PERSON-PHONE-NUMBER (1:3) '-'
PERSON-PHONE-NUMBER (4:4)
ELSE
DISPLAY 'Error updating phone number'
END-IF
END-IF.


EOJ-ROUTINE.
CLOSE PERSON-FILE.


GET-PERSON-ITEM.
MOVE 'N' TO INVALID-PERSON-KEY-FLAG.
READ PERSON-FILE
INVALID KEY
MOVE 'Y' TO INVALID-PERSON-KEY-FLAG.


REPLACE-PERSON-ITEM.
MOVE 'N' TO INVALID-PERSON-KEY-FLAG.
REWRITE PERSON-ITEM
INVALID KEY
MOVE 'Y' TO INVALID-PERSON-KEY-FLAG.

Program to lists all records in the file in sequential order.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PERSON-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\I\PERSON.DAT'
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS PERSON-KEY
ALTERNATE RECORD KEY IS PERSON-ALTERNATE-KEY
WITH DUPLICATES
FILE STATUS IS IO-FILE-STATUS.

DATA DIVISION.
FILE SECTION.

FD PERSON-FILE
DATA RECORD IS PERSON-ITEM.

01 PERSON-ITEM.
02 PERSON-KEY.
03 PERSON-ID PICTURE X(4).
02 PERSON-ALTERNATE-KEY.
03 PERSON-NAME PICTURE X(20).
02 PERSON-ADDRESS PICTURE X(20).
02 PERSON-CITY PICTURE X(18).
02 PERSON-STATE PICTURE X(2).
02 PERSON-ZIP PICTURE X(5).
02 PERSON-PHONE.
03 PERSON-AREA-CODE PICTURE X(3).
03 PERSON-PHONE-NUMBER PICTURE X(7).
02 PERSON-STATUS PICTURE X.

WORKING-STORAGE SECTION.

01 KBD-LETTER PIC X.

01 INVALID-PERSON-KEY-FLAG PICTURE X.
88 INVALID-PERSON-KEY VALUE 'Y'.
88 VALID-PERSON-KEY VALUE 'N'.

01 IO-FILE-STATUS PICTURE X(2).

PROCEDURE DIVISION.

MAIN-LINE.
PERFORM INITIALIZATION-ROUTINE.
PERFORM THE-LOOP
UNTIL INVALID-PERSON-KEY.
PERFORM EOJ-ROUTINE.
STOP RUN.

INITIALIZATION-ROUTINE.
OPEN I-O PERSON-FILE.
PERFORM GET-NEXT-PERSON-ITEM.

THE-LOOP.
DISPLAY PERSON-NAME.
DISPLAY PERSON-ADDRESS.
DISPLAY PERSON-CITY ' ' PERSON-STATE ' ' PERSON-ZIP.
DISPLAY '(' PERSON-AREA-CODE ') '
PERSON-PHONE-NUMBER (1:3) '-'
PERSON-PHONE-NUMBER (4:4).
DISPLAY '------------------------------'.
PERFORM GET-NEXT-PERSON-ITEM.


EOJ-ROUTINE.
CLOSE PERSON-FILE.

GET-NEXT-PERSON-ITEM.
MOVE 'N' TO INVALID-PERSON-KEY-FLAG.
READ PERSON-FILE NEXT
AT END
MOVE 'Y' TO INVALID-PERSON-KEY-FLAG.