FILE MATCH程序在COBOL中经常会遇到,可以说占了很大部分,这里就个人经验总结一下FILE MATCH程序的二个最常用的模板。
为简化起见,这里只写出程序的主逻辑。
假定file1和file2做match,有下面两种常见的情况:
1.一对一做match,即file1:file2 = 1:1
PERFORM ITF1-READ-RTN /*读ITF1
PERFORM ITF2-READ-RTN /*读ITF2
PERFORM TEST BEFORE
UNTIL (ITF1-KEY = LOW-VALUE) AND /*在读完两个文件后需对KEY进行赋LOW-VALUE,用KEY做循环终止条件
(ITF2-KEY = LOW-VALUE)
EVALUATE TRUE
WHEN ITF1-KEY < ITF2-KEY
PERFORM OTF1-WRITE-RTN /*没MATCH上时写OTF1
PERFORM ITF1-READ-RTN
WHEN ITF1-KEY > ITF2-KEY
PERFORM OTF2-WRITE-RTN /*没MATCH上时写OTF2
PERFORM OTF2-READ-RTN
WHEN OTHER
PERFORM MATCH-RTN /*MATCH上时的处理
PERFORM OTF3-WRITE-RTN /*MATCH上时写OTF3
PERFORM ITF1-READ-RTN
PERFORM ITF2-READ-RTN
END-EVALUATE
END-PERFORM
2.一对多做match,即file1:file2 = 1:N
PERFORM ITF1-READ-RTN /*读ITF1
PERFORM ITF2-READ-RTN /*读ITF2
PERFORM TEST BEFORE
UNTIL (ITF1-KEY = LOW-VALUE) AND /*在读完两个文件后需对KEY进行赋LOW-VALUE,用KEY做循环终止条件
(ITF2-KEY = LOW-VALUE)
EVALUATE TRUE
WHEN ITF1-KEY < ITF2-KEY
PERFORM OTF1-WRITE-RTN /*没MATCH上时写OTF1
PERFORM ITF1-READ-RTN
WHEN ITF1-KEY > ITF2-KEY
PERFORM OTF2-WRITE-RTN /*没MATCH上时写OTF2
PERFORM OTF2-READ-RTN
WHEN OTHER
PERFORM TEST BEFORE
UNTIL (ITF1-KEY NOT = ITF2-KEY)
PERFORM MATCH-RTN /*MATCH上时的处理
PERFORM OTF3-WRITE-RTN /*MATCH上时写OTF3
PERFORM ITF2-READ-RTN
END-PERFORM
PERFORM ITF1-READ-RTN
END-EVALUATE
END-PERFORM
补充:上面两个程序模板是FILE MATCH程序最基本的两种形式,其他复杂的逻辑也可以根据它们来修改扩充。