从cobol中的字符串中删除特定值

时间:2018-11-06 17:33:05

标签: cobol

想问一下如何从字符串中删除不同长度的特定值。

我有这个: '{4:72:SELLS¬#:73:ABC¬#:PPF:TESTPPF¬#:74:BLA¬#:PPF:ABC¬#:74:BLA¬#-}'

(要删除所有¬#:PPF:标签及其内容。在我的示例中,应将其删除:

  • ¬#:PPF:TESTPPF
  • ¬#:PPF:ABC

并希望拥有以下内容: '{4:72:SELLS¬#:73:ABC¬#:74:BLA¬#:74:BLA¬#-}'

我有此代码:

01 TINP.
    05 TINPFIELD                         PIC  X(2000) VALUE
       '{4:72:SELLS¬#:73:ABC¬#:PPF:TESTPPF¬#:74:BLA¬#:PPF:TESTPPF¬#:74:BLA¬#-}'.

01 WA-OUTPUT    PIC X(2000) value spaces.
01 WA-TEMP      PIC X(2500).
01 WP-MSG       PIC 9(8) BINARY value zero.
01 WN-ROWCNT    PIC S9(8) BINARY.
01 WN-ROWCNT2   PIC S9(8) BINARY.
01 WP-BEG       PIC 9(8) BINARY.
01 WP-END       PIC 9(8) BINARY.
01 WN-OUT-LEN   PIC 9(8) BINARY value zero.

0000-TESTPROCESSING SECTION.

    display TINPFIELD.

    INSPECT TINPFIELD
            TALLYING WN-Rowcnt FOR ALL "¬#".

    MOVE 1 TO WP-MSG

    PERFORM UNTIL WN-ROWCNT2 >= WN-ROWCNT

      MOVE WP-MSG TO WP-BEG
      display 'WP-BEG' WP-BEG

      UNSTRING TINPFIELD
        DELIMITED BY "¬#"
        INTO WA-TEMP
        POINTER WP-MSG
      END-UNSTRING

      MOVE WP-MSG TO WP-END
      display 'WP-END' WP-END

      if WA-OUTPUT = space
        subtract 1 from wp-end
        STRING TINPFIELD(WP-BEG:WP-END)
               delimited by SIZE
               INTO WA-OUTPUT
        END-STRING
        move wp-end to WN-OUT-LEN
       else
         STRING WA-OUTPUT(1:WN-OUT-LEN)
                delimited by SIZE
                TINPFIELD(WP-BEG:WP-END)
                delimited by SIZE
                '¬#'
                delimited by SIZE
                INTO WA-OUTPUT
         END-STRING
       end-if

       move WP-END TO WN-OUT-LEN
       display 'WN-OUT-LEN' WN-OUT-LEN

       ADD 1 TO WN-ROWCNT2

     END-Perform
     .
     EXIT.

// edit:输入数据始终相同。在输出中(在我的代码运行之后),我有时会有两次“标记”,有时只有一次,依此类推。它根本不一致。我想这是我的代码问题。

1 个答案:

答案 0 :(得分:0)

更改了代码以反映问题的变化。

此代码将输入分成多个分隔的段,使用引用修改将每个段直接复制到输出。如果没有要删除的文本(没有定界符),它将直接将所有输入复制到输出中。

此方法对性能有影响。具体来说,每个段的移动都会在输出中引起空间填充。段数越大,性能越差。

我将WA-OUTPUT更改为X(2000),因为输出永远不会大于输入。

   01 TINP.
       05 TINPFIELD                         PIC  X(2000) VALUE
           '{4:72:SELLS¬#:73:ABC¬#:PPF:TESTPPF¬#:74:BLA' &
           '¬#:PPF:TESTPPF¬#:74:BLA¬#-}'.

   01 WA-OUTPUT    PIC X(2000) value spaces.
   01 segment-length binary pic 9(4).
   01 additional-characters binary pic 9(4).
   01 input-pointer binary pic 9(4).
   01 output-pointer binary pic 9(4).
   01 input-length binary pic 9(4).
   01 output-length binary pic 9(4).
   procedure division.
   begin.
       move 1 to input-pointer output-pointer
                 input-length output-length
       inspect TINPFIELD tallying
           input-length for characters before "}".
       display input-length
       display TINPFIELD
       perform until input-pointer > function length(TINPFIELD)
           unstring TINPFIELD delimited "¬#:PPF:"
               into WA-OUTPUT (output-pointer:)
               count in segment-length
               with pointer input-pointer
           add segment-length to output-pointer
           if input-pointer <= function length(TINPFIELD)
               move 0 to additional-characters
               inspect TINPFIELD (input-pointer:) tallying
                   additional-characters for characters before "¬"
               add additional-characters to input-pointer
           end-if
       end-perform
       inspect WA-OUTPUT tallying
           output-length for characters before "}".
       display wa-output
       display output-length
       goback
       .

输出:

0070
{4:72:SELLS¬#:73:ABC¬#:PPF:TESTPPF¬#:74:BLA¬#:PPF:TESTPPF¬#:74:BLA¬#-}
{4:72:SELLS¬#:73:ABC¬#:74:BLA¬#:74:BLA¬#-}
0042

这是对上述内容的修改,虽然稍微复杂一些,但消除了在有更多段时对空间填充带来的性能损失。这很不寻常,因为尽管它使用UNSTRING语句,但实际上并没有“解串”任何东西。

   01 TINP.
       05 TINPFIELD                         PIC  X(2000) VALUE
           '{4:72:SELLS¬#:73:ABC¬#:PPF:TESTPPF¬#:74:BLA' &
           '¬#:PPF:TESTPPF¬#:74:BLA¬#-}'.

   01 WA-OUTPUT    PIC X(2000) value spaces.
   01 segment-length binary pic 9(4).
   01 segment-holder pic x.
   01 additional-characters binary pic 9(4).
   01 input-pointer binary pic 9(4).
   01 backup-pointer binary pic 9(4).
   01 output-pointer binary pic 9(4).
   01 input-length binary pic 9(4).
   01 output-length binary pic 9(4).
   procedure division.
   begin.
       move 1 to input-pointer output-pointer
                 input-length output-length
       inspect TINPFIELD tallying
           input-length for characters before "}".
       display input-length
       display TINPFIELD
       perform until input-pointer > function length(TINPFIELD)
           move input-pointer to backup-pointer
           unstring TINPFIELD delimited "¬#:PPF:"
               into segment-holder
               count in segment-length
               with pointer input-pointer
           move TINPFIELD (backup-pointer:segment-length)
               to WA-OUTPUT (output-pointer:segment-length)
           add segment-length to output-pointer
           if input-pointer <= function length(TINPFIELD)
               move 0 to additional-characters
               inspect TINPFIELD (input-pointer:) tallying
                   additional-characters for characters before "¬"
               add additional-characters to input-pointer
           end-if
       end-perform
       if output-pointer < function length (WA-OUTPUT)
           move space to WA-OUTPUT (output-pointer:)
       inspect WA-OUTPUT tallying
           output-length for characters before "}".
       display wa-output
       display output-length
       goback
       .

输出是相同的。