Is there a way to use INSPECT TALLYING with a check for multiple characters?

18,500

You have pretty much answered your own question... The answer is yes you can do this. Here is a working example program:

  IDENTIFICATION DIVISION.
  PROGRAM-ID. EXAMPLE.
  DATA DIVISION.
  WORKING-STORAGE SECTION.
  01    WS-INPUT-STRING PIC X(80).
  01    WS-COUNTER      PIC 9(4).
  01    WS-TAG          PIC X(10).
  PROCEDURE DIVISION.
  MAIN-PARAGRAPH.
       MOVE 'askabanskarkartikrockstar' TO WS-INPUT-STRING
       MOVE ZERO                        TO WS-COUNTER
       MOVE 'kartik'                    TO WS-TAG
       INSPECT WS-INPUT-STRING
       TALLYING WS-COUNTER
       FOR CHARACTERS BEFORE WS-TAG(1:6)
       DISPLAY WS-COUNTER
       GOBACK
       .

WS-COUNTER displays as 11, there are 11 characters before the WS-TAG string.

Notice that I defined WS-TAG as PIC X(10). This variable is longer than the actual tag value you are looking for. To prevent the INSPECT verb from trying to match on trailing spaces introduced by:

      MOVE 'kartik' TO WS-TAG

I had to specify a reference modified value for INSPECT to search for. Had I simply used:

      FOR CHARACTERS BEFORE WS-TAG

without reference modification, WS-COUNTER would have been 80 - the length of WS-INPUT-STRING. This is because the string 'kartik ' is not found and the counter tallies the length of the entire input string.

Another approach would be to specify the tag as a literal:

      FOR CHARACTERS BEFORE 'kartik'

You can move hexadecimal constants into PIC X fields as follows:

      MOVE X'0D25' TO WS-TAG

This occupies 2 characters so you would use WS-TAG(1:2) when INSPECTing it.

Share:
18,500
ikartik90
Author by

ikartik90

I'm a programmer turned UX designer living in New Delhi, India. I love working on dashboards, creating wireframes, and solving challenging design problems through research and understanding. I generally use Photoshop, Illustrator and XD to create my designs and prototypes. Though I've even jumped on and off to other tools like Origami and InVision as needed. And when nothing seems to serve the purpose, I just pick up a few sheets of paper and my favorite ball point pen and start scribbling stuff down to figure something out.

Updated on June 05, 2022

Comments

  • ikartik90
    ikartik90 almost 2 years

    I have a string for which I wish to tally the count of characters till a certain pattern of characters is found.

    For example:

    Give a string: askabanskarkartikrockstar
    I would like to know how many characters are there before the kartik in the string.

    In a normal scenario where I need to find the number of characters before, say k, in the given string, I would write the code somewhat as:

    INSPECT WS-INPUT-STRING TALLYING CT-COUNTER FOR CHARACTERS BEFORE LT-K
    

    Where

    • WS-INPUT-STRING is alphanumeric with a value of askabanskarkartikrockstar,
    • CT-COUNTER is the counter used to count the number of characters
    • LT-K is a literal with the value k.

    But here, if I wish to do the same for a sub-string, like kartik in the above example, would replacing the value of LT-K with kartik instead of just k work? If yes, is the same applicable for alphanumeric literals that have values in the form of hexadecimal numbers (for example, in a literal X(02) one stores a new-line character as x'0D25')?

    I'm trying to implement the above code in zOS IBM mainframe v10. Thanks.

  • ikartik90
    ikartik90 over 11 years
    Hi @NealB, I tried using the above approach of specifying the literals in limited length variables, for example kartik would be held in a variable of length X(06) and X'0D25' would be held in an X(02) variable. The approach works fine with using alphanumeric values in the variable. The problem arises when I try to tally the counter for a hexadecimal coded alphanumeric value like X'0D25'. Here it seems to compare just the first character i.e. when I give X'0D' in an X(01) variable it works but when I try it for the X(02) variable with X'0D25' it fails to tally the right count.
  • NealB
    NealB over 11 years
    @ikartik90 Better take a second look at the actual contents of the variables you are comparing (maybe one of them contains double byte character data and the other single byte characters). I have tested INSPECT TALLYING using binary data (eg. find X'0D25' in X'0D00250D250025' and the tally comes back as 3, which is correct).
  • ikartik90
    ikartik90 over 11 years
    I get what you intend to mean and also, I have very definitely checked of the contents of the variables I am comparing. I'm very sure of its correctness. What startles me is the same issue that when it works while I test it for a substring why does it not work for the hex substring.
  • NealB
    NealB over 11 years
    @ikartik90 Try copying the program from this answer then MOVE the hex constants given in my previous comment into WS-INPUT-STRING and WS-TAG. I ran this test using IBM Enterprise COBOL for z/OS 4.2.0 and it works just fine.