0

我正在使用 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  

这种算法有点工作,但一些输出是在开始/结束处拼接的。所以关于这里有什么问题的任何想法或建议更好的东西?

4

2 回答 2

3

你看过 UNSTRING 吗?这似乎是为您的情况量身定制的。

MOVE 1 TO WS-SUB1
PERFORM UNTIL WS-SUB1 >= LENGTH OF WS-STRING
  UNSTRING WS-STRING DELIMITED SPACE
    INTO WS-WORD-OUT COUNT IN WS-WORD-LEN
    POINTER WS-SUB1
  END-UNSTRING
  ADD 1 TO WS-COUNT(2)
  PERFORM Z-400-OUTPUT-WORD
  ADD WS-WORD-LEN TO WS-SUB1
END-PERFORM

注意:代码只是徒手写的,未经编译且未经测试。

于 2012-07-03T11:47:07.430 回答
2

@cschneid 是对的。UNSTRING是要走的路。如果您希望将多个空格视为单个分隔符,请使用 DELIMITED BY ALL SPACES。在http://publib.boulder.ibm.com/infocenter/iadthelp/v7r0/index.jsp?topic=/com.ibm.etools.iseries.langref.doc/c0925395564.htm有一个例子。

如果您稍后需要将它们重新组合在一起,您可以使用 STRING 动词来完成。我建议您还使用 ON OVERFLOW 子句来捕获诸如太长而无法放入目标变量的单词之类的内容。它发生了。

于 2012-07-03T16:10:31.317 回答