Menu

Is this a BUG ?? - Substitute command.

David Wall
2021-11-24
2021-11-24
  • David Wall

    David Wall - 2021-11-24

    I have a string field XXX with the following data.: ++ are blanks. Difficult to represent here.
    ++++++++GO+NXT+NXT+NXT+DEPENDING+ON+NXTNUM.
    I want to change the NXT fields for ABC such that the line now says:
    ++++++++GO+ABC+ABC+ABC+DEPENDING+ON+NXTNUM.

    Now - noting that I don't want to change NXTNUM to become ABCNUM I did the following.
    Inspect XXX replacing all "+" by "@". ie: change all spaces to @ signs.
    now I did a: MOVE Substitute (XXX "@NXT@" "@ABC@") to YYY.
    followed by a reverse: Inspect YYY replacing all "@" by "+". ie: change all the @ signs back to spaces.
    and got the following result:
    ++++++++GO+ABC+NXT+ABC+DEPENDING+ON+NXTNUM.

    The question is - should the substitute NOT HAVE ALSO changed the middle NXT to ABC ??.

    The manual says the following:

    This function parses string, replacing all occurrences of from-n strings with the corresponding
    to-n strings. My Highlighting.

    Sample program duplicating the above with comments attached.

     
  • Emmad

    Emmad - 2021-11-24

    It looks like you are correct. Maybe after the first replace, the pointer that records current position in the from-string gets incremented by 1 and does not recognize @NXT@ as it is but recognize it as NXT@ only, accordingly it does not replace it.
    I tried to convert what you said into a small test program. The last case shows a simpler version of the problem:

    IDENTIFICATION DIVISION.
    PROGRAM-ID. test.
    DATA DIVISION.
    WORKING-STORAGE SECTION.
    01 xxx           pic x(40).
    01 YYY           pic x(40).
    01 ORIGINAL-XXX  PIC X(40).
    PROCEDURE DIVISION.
    
    MOVE "    GO NXT NXT NXT DEPENDING ON NXTCNT. " TO ORIGINAL-XXX
    MOVE ORIGINAL-XXX TO XXX
    
    MOVE FUNCTION SUBSTITUTE (XXX "NXT" "ABC") TO YYY.
    
    DISPLAY "1: " YYY
     *> YYY = GO ABC ABC ABC DEPENDING ON ABCCNT. "
    
     INSPECT XXX REPLACING ALL " " BY "@".
     DISPLAY "2: " XXX
     *> @@@@GO@NXT@NXT@NXT@DEPENDING@ON@NXTCNT.@
     MOVE FUNCTION SUBSTITUTE (XXX "@NXT@" "@ABC@") TO YYY.
     DISPLAY "3: " YYY 
     *>  @@@@GO@ABC@NXT@ABC@DEPENDING@ON@NXTCNT.@
     *> EXPECTED
     *>  @@@@GO@ABC@ABC@ABC@DEPENDING@ON@NXTCNT.@
     *>            ---------              
     INSPECT YYY REPLACING ALL "@" BY " ".
    DISPLAY "4: " YYY
    *>      GO ABC NXT ABC DEPENDING ON NXTCNT. 
    
    *> My Guess:
    *> IF AN EXTRA @ IS USED FOLLOWING NXT, IT WORKS
    MOVE "@@@NXT@@NXT@" TO XXX
    MOVE FUNCTION SUBSTITUTE (XXX "@NXT@" "@ABC@") TO YYY.
    DISPLAY YYY
    *> @@@ABC@@ABC@
    STOP RUN.
    
     
  • Simon Sobisch

    Simon Sobisch - 2021-11-24

    Works "as designed", which is like Emmad guessed a "pointer wise" change; text that already got replaced - in your case the trailing @ of @NXT@ - should not be re-inspected.

    Note: In this special case (length are identical) I'd suggest to use INSPECT instead of FUNCTION SUBSTITUTE, but if you need to inspect the full context left/right you may want to loop over the text.

     
    • David Wall

      David Wall - 2021-11-24

      Inspect replacing fails the same way. I end up with ABC NXT ABC.
      Perhaps the 'as designed' needs a repair.
      as the manual says replace all occurrences - it's an occurrence - it just falls as part of an existing occurrence.

      OK - hows about I pose this question.

      A field contains the following - where a + is actually a space.
      How do I replace ALL occurrences of NXT (ONLY) with ABC.

      ++++++GO+NXT+NXT+NXT+DEPENDING+ON+NXTCNT that gives me
      ++++++GO+ABC+ABC+ABC+DEPENDING +ON+NXTCNT.

       
      • Simon Sobisch

        Simon Sobisch - 2021-11-24

        I assume you don't meant the manual but the Programmer's Guide. In this case please create a bug report for Vince to adjust it, ideally with a suggestion how it should be changed.

         
    • David Wall

      David Wall - 2021-11-24

      YES as you say - looping over does the trick - eventually.

       
      • Simon Sobisch

        Simon Sobisch - 2021-11-24

        Either looping or applying SUBSTITUTE more than once (three times should work for everything).

         
  • David Wall

    David Wall - 2021-11-24

    At least 1 thing I've learned from this - you can Move substitute (AAA BBB CCC) to AAA.
    Source & Destination can be the same.
    I've always done the move from AAA to XXX & then moved back again.

     

Anonymous
Anonymous

Add attachments
Cancel