您可能希望将 SORT 与 INPUT PROCEDURE 和 OUTPUT PROCEDURE 子句一起使用。
下面的示例仅用于提示,但应导致可接受的解决方案。在这些过程中,您几乎可以完全控制何时以及如何将输入记录释放到排序文件,以及如何通过输出过程管理 RETURN 记录。
为了有趣和打字,您可以将它们链接为过滤器,或者将少数类型转换为枚举并将值填充到单个 INPUT PROCEDURE IS 传递的主键中。您可以跳过 OUTPUT PROCEDURE IS 短语并混合在 GIVING 中,或者玩得很好并使用 OUTPUT PROCEDURE 将枚举转换回人类可读的标签。
OCOBOL >>SOURCE FORMAT IS FIXED
******************************************************************
* Date: 02-Sep-2008
* Purpose: An OpenCOBOL SORT verb example
* Tectonics: cobc -x sorting.cob
* ./sorting <input >output
* or simply
* ./sorting
* for keyboard and screen demos
******************************************************************
identification division.
program-id. sorting.
environment division.
configuration section.
* This sets up a sort order lower then upper except for A and a
special-names.
alphabet mixed is " AabBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTu
-"UvVwWxXyYzZ0123456789".
input-output section.
file-control.
select sort-in
assign keyboard
organization is line sequential.
select sort-out
assign display
organization is line sequential.
select sort-work
assign "sortwork".
data division.
file section.
fd sort-in.
01 in-rec pic x(255).
fd sort-out.
01 out-rec pic x(255).
sd sort-work.
01 work-rec pic x(255).
working-storage section.
01 loop-flag pic x value low-value.
procedure division.
sort sort-work
on descending key work-rec
collating sequence is mixed
input procedure is sort-transform
output procedure is output-uppercase.
display sort-return end-display.
goback.
******************************************************************
sort-transform.
move low-value to loop-flag
open input sort-in
read sort-in
at end move high-value to loop-flag
end-read
perform
until loop-flag = high-value
move FUNCTION LOWER-CASE(in-rec) to work-rec
release work-rec
read sort-in
at end move high-value to loop-flag
end-read
end-perform
close sort-in
.
******************************************************************
output-uppercase.
move low-value to loop-flag
open output sort-out
return sort-work
at end move high-value to loop-flag
end-return
perform
until loop-flag = high-value
move FUNCTION UPPER-CASE(work-rec) to out-rec
write out-rec end-write
return sort-work
at end move high-value to loop-flag
end-return
end-perform
close sort-out
.
exit program.
end program sorting.
有关详细信息,请参阅http://opencobol.add1tocobol.com/#sort 。