Search code examples
ibm-midrangecobol

iSeries COBOL - TALLYING BEFORE/AFTER


i have a field that have some tag inside and I must extract the values between this tags. String example:

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

So I must extract for example the value between "/33=" and "/34="... in this case 10000 In my cobol source I can have the first because it count all chars before "/33=":

INSPECT MSG TALLYING COUNT1 FOR CHARACTERS BEFORE "/33="
ADD 5 TO COUNT1

But I don't understand why is not work for the second the should give me nr. of chars I must estract:

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

The COUNT2 give me 0

Anybody can help me please ? Thanks in advance Denis


Solution

  • You could try to use PERFORM UNTIL, where you could use INSPECT TALLYING to know the position of the slash. It would be like to ask for the index in a string.

    All of it supposing you are needing it with variable length, otherwise to use a fixed structure is the best.

    If I understood what you need, here it is:

    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.
    
    

    INPUT:

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

    OUTPUT:

    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
    

    If you find any mistake, please, let me know.