著者:比毛 寛之
レガシー問題や技術者不足などで話題の多いプログラミング言語が「COBOL」です 。古い言語というイメージがありますが、ISOから2023年の新規格が公表されたことでも分かるように、現在も進化を続けています。本特集では、COBOLの現状を紹介しつつ、「opensource COBOL 4J」というオープンソースソフトウエアのCOBOLコンパイラを使ってCOBOL言語の基本を解説します。
シェルスクリプトマガジン Vol.86は以下のリンク先でご購入できます。
図1 「HELLO WORLD!」という文字列を表示するCOBOLのサンプルコード
1 2 3 4 5 6 7 8 9 10 11 12 |
----+----1----+----2----+----3----+----4----+----5 IDENTIFICATION DIVISION. PROGRAM-ID. HELLOWORLD. DATA DIVISION. WORKING-STORAGE SECTION. 01 MY-TEXT PIC X(20). PROCEDURE DIVISION. MAIN-RTN. MOVE "HELLO WORLD!" TO MY-TEXT. DISPLAY MY-TEXT. STOP RUN. ----+----1----+----2----+----3----+----4----+----5 |
図4 COBOLのサンプルコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8 IDENTIFICATION DIVISION. PROGRAM-ID. MYCOBOL. DATA DIVISION. WORKING-STORAGE SECTION. 01 MY-GRP. * 03 MY-WORK-X PIC X(10). 20230804 03 MY-WORK-X2 PIC X(10). 20230804 03 MY-WORK-9 PIC 9(05). ******************************************** PROCEDURE DIVISION. ******************************************** PROC1 SECTION. PROC1-000. * MOVE "ABC" TO MY-WORK-X. 20230804 MOVE "DEF" TO MY-WORK-X2. 20230804 MOVE 100 TO MY-WORK-9. PROC1-900. STOP RUN. ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8 |
図5 コンソールからの入力と画面への出力をするサンプルコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
----+----1----+----2----+----3----+----4----+----5----+ IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. DATA DIVISION. WORKING-STORAGE SECTION. 01 MY-NAME PIC X(20). PROCEDURE DIVISION. MAIN-RTN. DISPLAY "Enter your name: " NO ADVANCING. ACCEPT MY-NAME. DISPLAY "Hello " MY-NAME. MAIN-EXIT. STOP RUN. ----+----1----+----2----+----3----+----4----+----5----+ |
図6 ファイルにデータを出力するサンプルコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
----+----1----+----2----+----3----+----4----+----5----+----6----+----7 IDENTIFICATION DIVISION. PROGRAM-ID. EMPWRITE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMP-FILE ASSIGN TO "EMPFILE" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS EMP-CD FILE STATUS IS EMP-STS. DATA DIVISION. FILE SECTION. FD EMP-FILE. 01 EMP-REC. 03 EMP-CD PIC X(04). 03 EMP-NAME PIC X(20). 03 EMP-DPT-CD PIC X(02). 03 EMP-ENT-DATE PIC 9(08). WORKING-STORAGE SECTION. 01 EMP-STS PIC 9(02). PROCEDURE DIVISION. MAIN-CONTROL SECTION. MAIN-000. DISPLAY "*** Creating Employee file ***". OPEN OUTPUT EMP-FILE. * MOVE "0011" TO EMP-CD. MOVE "Saitama Saburo" TO EMP-NAME. MOVE "01" TO EMP-DPT-CD. MOVE 20020401 TO EMP-ENT-DATE. WRITE EMP-REC. * ----+----+----+----+----+----+----+ WRITE EMP-REC FROM "0012Chiba Jiro 0219990401". WRITE EMP-REC FROM "0013Tokyo Taro 0319970401". WRITE EMP-REC FROM "0014Kanagawa Shiro 0120120401". WRITE EMP-REC FROM "0015Niigata Goroo 0220010401". * ----+----+----+----+----+----+----+ CLOSE EMP-FILE. MAIN-900. STOP RUN. ----+----1----+----2----+----3----+----4----+----5----+----6----+----7 |
図7 ファイル中のレコードを読み出して画面に表示するサンプルコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
----+----1----+----2----+----3----+----4----+----5----+----6----+ IDENTIFICATION DIVISION. PROGRAM-ID. EMPLIST. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMP-FILE ASSIGN TO "EMPFILE" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS EMP-CD FILE STATUS IS EMP-STS. DATA DIVISION. FILE SECTION. FD EMP-FILE. 01 EMP-REC. 03 EMP-CD PIC X(04). 03 EMP-NAME PIC X(20). 03 EMP-DPT-CD PIC X(02). 03 EMP-ENT-DATE PIC 9(08). WORKING-STORAGE SECTION. 01 EMP-STS PIC 9(02). 01 DSP-REC. 03 DSP-CD PIC X(04). 03 FILLER PIC X. 03 DSP-NAME PIC X(20). 03 FILLER PIC XX. 03 DSP-DPT-CD PIC X(02). 03 FILLER PIC X. 03 DSP-ENT-DATE PIC 9999/99/99. PROCEDURE DIVISION. MAIN-CONTROL SECTION. MAIN-000. OPEN INPUT EMP-FILE. DISPLAY "*** Employee List ***". DISPLAY "ID Employee Name Dpt Enter date". DISPLAY "---- -------------------- --- ----------". PERFORM UNTIL (EMP-STS NOT = ZERO) READ EMP-FILE NEXT AT END DISPLAY "EOF" NOT AT END MOVE EMP-CD TO DSP-CD MOVE EMP-NAME TO DSP-NAME MOVE EMP-DPT-CD TO DSP-DPT-CD MOVE EMP-ENT-DATE TO DSP-ENT-DATE DISPLAY DSP-REC END-READ END-PERFORM. CLOSE EMP-FILE. MAIN-900. STOP RUN. ----+----1----+----2----+----3----+----4----+----5----+----6----+ |
図9 「EMPSEARCH.cbl」ファイルに記述するコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
----+----1----+----2----+----3----+----4----+----5----+----6----+- IDENTIFICATION DIVISION. PROGRAM-ID. EMPSEARCH. DATA DIVISION. WORKING-STORAGE SECTION. 01 WK-AREA. 03 WK-CD PIC X(04). 03 WK-NAME PIC X(20). 03 WK-DPT-CD PIC X(02). 03 WK-ENT-DATE PIC 9(08). 03 WK-RETURN PIC 9(01). PROCEDURE DIVISION. MAIN-RTN. DISPLAY "*** Employee Search ***". DISPLAY "Code: : " NO ADVANCING. ACCEPT WK-CD. CALL "EMPREAD" USING WK-CD, WK-NAME, WK-DPT-CD, WK-ENT-DATE, WK-RETURN. IF WK-RETURN = ZERO DISPLAY "Name : " WK-NAME DISPLAY "Dept code : " WK-DPT-CD DISPLAY "Enter date: " WK-ENT-DATE ELSE DISPLAY "Employee not found!" END-IF. MAIN-EXIT. STOP RUN. ----+----1----+----2----+----3----+----4----+----5----+----6----+- |
図10 「EMPREAD.cbl」ファイルに記述するコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
----+----1----+----2----+----3----+----4----+----5----+----6----+----7 IDENTIFICATION DIVISION. PROGRAM-ID. EMPREAD. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMP-FILE ASSIGN TO "EMPFILE" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS EMP-CD FILE STATUS IS EMP-STS. DATA DIVISION. FILE SECTION. FD EMP-FILE. 01 EMP-REC. 03 EMP-CD PIC X(04). 03 EMP-NAME PIC X(20). 03 EMP-DPT-CD PIC X(02). 03 EMP-ENT-DATE PIC 9(08). WORKING-STORAGE SECTION. 01 EMP-STS PIC 9(02). LINKAGE SECTION. 01 LK-CD PIC X(04). 01 LK-NAME PIC X(20). 01 LK-DPT-CD PIC X(02). 01 LK-ENT-DATE PIC 9(08). 01 LK-RETURN PIC 9(01). PROCEDURE DIVISION USING LK-CD, LK-NAME, LK-DPT-CD, LK-ENT-DATE, LK-RETURN. MAIN-CONTROL SECTION. MAIN-000. INITIALIZE EMP-REC. MOVE ZERO TO LK-RETURN. OPEN INPUT EMP-FILE. MOVE LK-CD TO EMP-CD. READ EMP-FILE KEY IS EMP-CD INVALID KEY MOVE 1 TO LK-RETURN END-READ. MOVE EMP-NAME TO LK-NAME. MOVE EMP-DPT-CD TO LK-DPT-CD. MOVE EMP-ENT-DATE TO LK-ENT-DATE. CLOSE EMP-FILE. MAIN-900. EXIT PROGRAM. ----+----1----+----2----+----3----+----4----+----5----+----6----+----7 |
図11 「EmpSearchDemo.java」ファイルに記述するコード
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import jp.osscons.opensourcecobol.libcobj.ui.*; public class EmpSearchDemo { public static void main(String[] args) throws Exception { EMPREAD prog = new EMPREAD(); CobolResultSet rs = prog.execute("0011", "", "", 0, 0); System.out.println("*** Employee Search from Java ***"); System.out.println("Code : " + rs.getString(1)); System.out.println("Name : " + rs.getString(2)); System.out.println("Dept code : " + rs.getString(3)); System.out.println("Enter date: " + rs.getInt(4)); } } |