Menu

Why does INSPECT DO THIS?

Anonymous
2021-07-28
2021-07-29
  • Anonymous

    Anonymous - 2021-07-28

    I expected either all cases below to return the same value for the variable C.
    Could some one give me a hint on why C is not 1 in all three cases please. Thanks.

    ...
           01  LS-IN-TXT               PIC  X(256).
           01  LS-IN-NEEDLE            PIC  X(256).
           01  C                       PIC  S999.
    ...
    
    
        MOVE "ABC DEF GHI" TO LS-IN-TXT
        MOVE "EF"          TO LS-IN-NEEDLE
    
        *> CASE A - THIS RETURNS C=2 - I EXPECTED C=1
          MOVE 0 TO C
        INSPECT LS-IN-TXT TALLYING C FOR ALL "EF"
    
        *> CASE B - THIS DOES NOT WORK...I.E. C=0 - I EXPECTED C=1
        MOVE 0 TO C
        INSPECT LS-IN-TXT TALLYING C FOR ALL LS-IN-NEEDLE
    
        *> CASE B - THIS DOES NOT WORK...I.E. C=0 - I EXPECTED C=1
        MOVE 0 TO C
        INSPECT LS-IN-TXT TALLYING C FOR ALL
                LS-IN-NEEDLE(1:FUNCTION LENGTH(LS-IN-NEEDLE)) 
     STOP RUN.
    
     
  • Simon Sobisch

    Simon Sobisch - 2021-07-28

    It does not work because om the first case you ask the INSPECT to check for exactly two characters, in the second and third case you ask it to check for 256 characters (it therefore will stop after finding the first one to not match).
    You possibly want to use FUNCTION TRIM (LS-IN-NEEDLE TRAILING) ...

     
    • David Wall

      David Wall - 2021-07-28

      Simon - could you expand on why the first case returned a 2 ?.
      I also would have expected it to return a 1.

      Surely there was 'only' 1 occurrence of the string "EF" in the other string.

      I understand why the second and third cases returned a 0.

      Actually - I just knocked up a simple test & got a 001 for case 1 - why would Anon get a 2 ?.

       

      Last edit: David Wall 2021-07-28
      • Anonymous

        Anonymous - 2021-07-29

        Appologies for "2" - I am not sure how did this happen. I re-tested this and it returns 1 only as expected not 2. i must have had a lot of displays in the code and I was confused. Thanks.

        mod edit, removed a few initial spaces to avoid a code block mode

         

        Last edit: Brian Tiffin 2021-07-29
  • Anonymous

    Anonymous - 2021-07-29

    Thank you simon. In the above example, the first case returns 1. I was wrong. Sorry.

     

Anonymous
Anonymous

Add attachments
Cancel