iSeries COBOL - 计数 BEFORE/AFTER

iSeries COBOL - TALLYING BEFORE/AFTER

我有一个字段,里面有一些标签 我必须提取这个标签之间的值。 字符串示例:

MSG="HERE IS THE TAGS /33=10000/34=36363/35=22222"

所以我必须提取例如“/33=”和“/34=”之间的值...在本例中为 10000 在我的 cobol 源代码中,我可以使用第一个,因为它计算了 "/33=":

之前的所有字符
INSPECT MSG TALLYING COUNT1 FOR CHARACTERS BEFORE "/33="
ADD 5 TO COUNT1

但我不明白为什么第二个不起作用应该给我 nr。我必须提取的字符数:

INSPECT MSG TALLYING COUNT2 FOR CHARACTERS AFTER "/33=" BEFORE "/"

COUNT2给我0

有人可以帮助我吗? 提前致谢 丹尼斯

INSPECT 语句不起作用,因为“BEFORE '/'”定位第一个斜杠,即“/33=”中的第一个斜杠,用于计数 0。

您可以尝试使用 PERFORM UNTIL,在这里您可以使用 INSPECT TALLYING 来了解斜线的位置。这就像在字符串中请求索引。

所有这些假设你需要它是可变长度的,否则使用固定结构是最好的。

如果我了解您的需求,请看这里:

WORKING STORAGE.
  01   TAB-OF-PARSED-INFO OCCURS 1 TO *[the max number of fields that you estimate]*
                         DEPENDING ON WS-I.  ---> this can be omitted with fixed occurs
       05 TAB-ITS-TAG        PIC X(04).
       05 TAB-WHAT-YOU-NEED  PIC X(*[the max size between slashes]*).
  01   WS-YOUR-STRING  PIC X(*[the max total size]*).
  01   WS-COUNTERS.
       05   WS-I            PIC 9(06).
       05   WS-COUNTER      PIC 9(06).
       05   WS-INDEX        PIC 9(06).
  01   WS-WHAT-SEPARATES    PIC X(01) VALUE '/'.
  01   WS-TIME-FLAG         PIC 9(01) VALUE 0.
       88  WS-FIRST-TIME              VALUE 0.
       88  WS-AFTER-TIME              VALUE 1.
...
  PROCEDURE DIVISION.
       
       INITIALIZE WS-COUNTERS

       INSPECT WS-YOUR-STRING 
       TALLYING WS-COUNTER FOR ALL WS-WHAT-SEPARATES

       PERFORM 1234-PARSING-SLASHES
          THRU 1234-PARSING-SLASHES-EXIT
         UNTIL WS-I >= WS-COUNTER
       ADD  1              TO WS-I
       MOVE WS-YOUR-STRING TO TAB-WHAT-YOU-NEED(WS-I)
       .
  1234-PARSING-SLASHES.
       MOVE ZEROES        TO WS-INDEX
       INSPECT WS-YOUR-STRING
       TALLYING WS-INDEX FOR ALL CHARACTERS BEFORE WS-WHAT-SEPARATES
       ADD  1             TO WS-I
       IF WS-FIRST-TIME THEN
          MOVE 'N/A'                      TO TAB-TAG(WS-I)
          MOVE WS-YOUR-STRING(1:WS-INDEX) TO TAB-WHAT-YOU-NEED(WS-I)
          SET WS-AFTER-TIME               TO TRUE
       ELSE
          MOVE WS-YOUR-STRING(1:3)        TO TAB-TAG(WS-I)
          MOVE WS-YOUR-STRING(4:WS-INDEX) TO TAB-WHAT-YOU-NEED(WS-I)
       END-IF
          
*    We adding 2 because WS-INDEX is the last non slash character. 
*    WS-INDEX + 1 is the slash
       ADD 2                              TO WS-INDEX
     
       COMPUTE WS-LEN      = LENGTH OF WS-YOUR-STRING
       MOVE WS-YOUR-STRING(WS-INDEX:WS-LEN) 
                                       TO WS-YOUR-STRING
      .
  1234-PARSING-SLASHES-EXIT.
  EXIT.


输入:

WS-YOUR-STRING="HERE IS THE TAGS /33=10000/34=36363/35=22222"

输出:

WS-I                  = 01
TAB-TAG(01)           = N/A
TAB-WHAT-YOU-NEED(01) = HERE IS THE TAG 

WS-I                  = 02
WS-YOUR-STRING        = 33=10000/34=36363/35=22222
TAB-TAG(02)           = 33=
TAB-WHAT-YOU-NEED(02) = 10000

WS-I                  = 03
WS-YOUR-STRING        = 34=36363/35=22222
TAB-TAG(03)           = 34=
TAB-WHAT-YOU-NEED(03) = 36363

WS-I                  = 04
WS-YOUR-STRING        = 35=22222
TAB-TAG(04)           = 35=
TAB-WHAT-YOU-NEED(04) = 22222

如果您发现任何错误,请告诉我。