GnuCOBOL

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-COLORS.
    03 MY-COLOR OCCURS 3 PIC X(10).
PROCEDURE DIVISION.
  MAIN SECTION.
    MOVE "red" TO MY-COLOR(1).
    MOVE "yellow" TO MY-COLOR(2).
    MOVE "blue" TO MY-COLOR(3).
    DISPLAY MY-COLORS.
    DISPLAY MY-COLOR(2).
    STOP RUN.
[vagrant@localhost cobol]$ cobc -x --free hello.cob
[vagrant@localhost cobol]$ ./hello
red       yellow    blue
yellow

演算

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-DATA.
    03 D1 PIC 99999 VALUE 0.
    03 D2 PIC 99999 VALUE 0.
    03 D3 PIC 99999 VALUE 0.
    03 D4 PIC 99999 VALUE 0.
PROCEDURE DIVISION.
  MAIN SECTION.
    ADD 5 TO D1 D2 D3.
    SUBTRACT 2 FROM D1 D2.
    DISPLAY MY-DATA.
    STOP RUN.

入力受付

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-DATA PIC X(10).
PROCEDURE DIVISION.
  MAIN SECTION.
    DISPLAY "whats your name?".
    ACCEPT MY-DATA.
    DISPLAY "hello! " MY-DATA.
    STOP RUN.

条件分岐

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-SCORE PIC 9(3).
PROCEDURE DIVISION.
  MAIN SECTION.
    MOVE 80 TO MY-SCORE.
    If MY-SCORE > 60 THEN
      DISPLAY "OK!"
    END-IF.
    STOP RUN.

条件分岐

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-SIGNAL PIC X(5).
PROCEDURE DIVISION.
  MAIN SECTION.
    MOVE "red" TO MY-SIGNAL.
    EVALUATE MY-SIGNAL
      WHEN "red"
        DISPLAY "stop!"
      WHEN "blue"
          DISPLAY "run!"
      WHEN "yello"
            DISPLAY "caution!"
    END-EVALUATE.
    STOP RUN.

繰り返し処理

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-COUNTER PIC 9(3) VALUE 1.
PROCEDURE DIVISION.
  MAIN SECTION.
    PERFORM 10 TIMES
      DISPLAY "counter = " MY-COUNTER
      ADD 1 TO MY-COUNTER
    END-PERFORM.
    STOP RUN.

ファイルの書き込み

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
  INPUT-OUTPUT SECTION.
    FILE-CONTROL.
      SELECT OUT-FILE ASSIGN TO 'out.txt'
        ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
  FILE SECTION.
    FD OUT-FILE.
      01 OUT-FILE-REC PIC 99.
 WORKING-STORAGE SECTION.
  01 LINE-COUNT PIC 99 VALUE 0.
PROCEDURE DIVISION.
  MAIN SECTION.
    OPEN OUTPUT OUT-FILE.

    PERFORM 10 TIMES
      ADD 1 TO LINE-COUNT
      MOVE LINE-COUNT TO OUT-FILE-REC
      WRITE OUT-FILE-REC
      DISPLAY OUT-FILE-REC
    END-PERFORM.

    CLOSE OUT-FILE.
    STOP RUN.

ファイルの読み出し

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
  INPUT-OUTPUT SECTION.
   FILE-CONTROL.
    SELECT IN-FILE ASSIGN TO 'in.txt'
     ORGANIZATION IS LINE SEQUENTIAL
     STATUS IN-FILE-STATUS.
DATA DIVISION.
  FILE SECTION.
   FD IN-FILE.
    01 In-FILE-REC PIC X(20).
  WORKING-STORAGE SECTION.
   01 IN-FILE-STATUS PIC XX.
PROCEDURE DIVISION.
  MAIN SECTION.
    OPEN INPUT IN-FILE.

    PERFORM UNTIL IN-FILE-STATUS NOT = "00"
     READ IN-FILE
       AT END
         DISPLAY "-- end --"
       NOT AT END
         DISPLAY "Content: " IN-FILE-REC
    END-READ
    END-PERFORM.

    CLOSE IN-FILE.
    STOP RUN.

OPEN-COBOL

COBOLはcommon business oriented languageの略で、書式のルールが厳格です。文末がピリオドで、大文字です。

DIVISION > SECTION

cobol

開発環境に必要となるファイルをインストールしていきましょう。

# yum -y install gcc
# yum -y install gmp-devel
# yum -y install ncurses-devel
# yum -y install db4-devel
# wget http://downloads.sourceforge.net/open-cobol/open-cobol-1.0.tar.gz
# tar zxvf open-cobol-1.0.tar.gz
# cd open-cobol-1.0
# ./configure
# make
# make install
# ln -s /usr/local/lib/libcob.so.1.0.0 /usr/lib/libcob.so.1
# ln -s /usr/local/lib/libcob.so.1.0.0 /usr/lib/libcob.so
# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/li

自由書式です。

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
  MAIN SECTION.
    DISPLAY 'HELLO WORLD!'.
    STOP RUN.
[vagrant@localhost cobol]$ cobc -x --free hello.cob
[vagrant@localhost cobol]$ ls
hello  hello.cob
[vagrant@localhost cobol]$ ./hello
HELLO WORLD!

変数

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-DATA1 PIC X(20).
PROCEDURE DIVISION.
  MAIN SECTION.
    MOVE 'HELLO WORLD!' TO MY-DATA1.
    DISPLAY MY-DATA1.
    STOP RUN.

数値の変数

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-DATA1 PIC X(20).
  01 MY-DATA2 PIC 99999.
PROCEDURE DIVISION.
  MAIN SECTION.
    MOVE 120 TO MY-DATA2.
    MOVE 'HELLO WORLD!' TO MY-DATA1.
    DISPLAY MY-DATA2.
    STOP RUN.

集合項目

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
 WORKING-STORAGE SECTION.
  01 MY-PRODUCT.
    03 PRODUCT-ID PIC X(5).
    03 PRODUCT-PRICE PIC 9(5).
PROCEDURE DIVISION.
  MAIN SECTION.
    MOVE "ID-12" TO PRODUCT-ID.
    MOVE 2980 TO PRODUCT-PRICE.
    DISPLAY PRODUCT-PRICE.
    DISPLAY MY-PRODUCT.
    STOP RUN.