我正在使用 TELON/COBOL atm,我需要一种算法来从字符串中提取单词。尝试搜索但找不到类似的东西。
无论如何,算法需要提取单词,并忽略空格;这是我到目前为止所拥有的(我为不熟悉 e 语法的人发表了评论)
WS-STRING 是输入字符串
WS-WORD-LEN 是要提取的单词的长度
WS-LST-WORD-P 是要提取的当前单词的起始位置(在字符串内)
WS-SUB1 是循环索引
PERFORM TEST BEFORE
VARYING WS-SUB1 FROM 1 BY 1
UNTIL WS-SUB1 > WS-STRING-LEN //loop for each char in the string, add 1 to WS-SUB1 in each itiaration
EVALUATE TRUE
WHEN WS-STRING(WS-SUB1:1) = SPACES //if the current char is a space
MOVE WS-SUB1 TO WS-SUB1-FRD
ADD 1 TO WS-SUB1-FRD
IF WS-STRING(WS-SUB1-FRD:1) = SPACES //Checks to see if the next char is a space
ADD 1 TO WS-LST-WORD-P
ELSE
MOVE WS-STRING(WS-LST-WORD-P:WS-WORD-LEN) //Substing "WS-WORD-LEN" many char from "WS-STRING" starting at "WS-LST-WORD-P" into "WS-WORD-OUT"
TO WS-WORD-OUT
ADD 1 TO WS-COUNT(2)
PERFORM Z-400-OUTPUT-WORD //This outputs "WS-WORD-OUT" to a file.
MOVE WS-SUB1 TO WS-LST-WORD-P
MOVE 1 TO WS-WORD-LEN
END-IF
WHEN OTHER
ADD 1 TO WS-WORD-LEN
END-EVALUATE
END-PERFORM
MOVE 1 TO WS-LST-WORD-P
这种算法有点工作,但一些输出是在开始/结束处拼接的。所以关于这里有什么问题的任何想法或建议更好的东西?