RAMESH KRISHNA REDDY
 Online Tutorials | PREV | TOP | NEXT

DRONA SERIES
COBOL STUDY MATERIAL

INSPECT

Author : Kishore B.
 ``` INSPECT INSPECT INSPECT verb allows to count and/or replace a character or a group of characters. INSPECT has options TALLYING, REPLACING & CONVERTING. INSPECT … with TALLYING option This form counts/tally a character or a group of characters in source string. INSPECT Examples: Source-string = “AABAbbACABA” 1. INSPECT source-string TALLYING tally-counter FOR CHARACTERS BEFORE INITIAL ‘C’ In this example tally counter will have count of all characters before first occurrence of ‘C’ in source-string. Tally-counter will be ‘7’. “AABAbbACABA” 2. INSPECT source-string TALLYING tally-counter FOR ALL ‘A’ In this example tally counter will have count of all occurrences of ‘A’ in source-string. Tally-counter will be ‘6’. “AABAbbACABA” 3. INSPECT source-string TALLYING tally-counter FOR ALL ‘A’ AFTER INITIAL ‘B’ In this example tally counter will have no of occurrences of ‘A’ after first occurrence of ‘B’. Tally-counter will be ‘4’. “AABAbbACABA” 4. INSPECT source-string TALLYING tally-counter FOR LEADING ‘A’ In this example tally counter will have no of leading A’s. Tally-counter will be ‘2’. “AABAbbACABA” 5. Source-string = " SSET" If we need to get string without spaces, trim the string. we can use following logic. INSPECT FUNCTION REVERSE (Source-string) TALLYING space-count FOR LEADING SPACES. COMPUTE length-of-string = 6 - space-count. Move Source-string(space-count+1 : length-of-string ) TO ws-target-string. Above INSPECT command get the no of leading spaces from the string. after executing the INSPECT command space-count variable contains 2. In compute statement, space-count subtracted from length of Source-string. value 4 will be stored in length-of-string. In move statement, Using referece modification, moved actual string to ws-target-string. removed spaces. INSPECT… with REPLACING option This form replaces a character or group of characters Example: Source-string = “AABAbbACABA” 1. INSPECT source-string REPLACING CHARACTERS BY ‘#’ BEFOR INITIAL ‘C’. In above example all characters before first occurrence of ‘C’ in source-string are replaced by ‘#’. Input : “AABAbbACABA” Output: -“#######CABA”. 2. INSPECT source-string REPLACING ALL ‘A’ BY ‘#’ In above example all occurrences of ‘A’ in source-string are replaced by ‘#’. Input : “AABAbbACABA” Output: - “##B#bb#C#B#”. TIPS : With CHARACTERS phrase only one character size of replacing string should be specified. And also delimiter (‘C’) must be of one character. Replacing string and replaced string must be of same size if we use phrases ALL, LEADING and FIRST. INSPECT… with TALLYING and REPLACING options This form counts/tally a character or group of characters and replaces a character or group of characters. Example: Source-string = “AABAbbACAB” 1. INSPECT source-string TALLYING tally-counter FOR ALL ‘A’ AFTER INITIAL ‘B’ REPLACING ALL ‘A’ BY ‘#’ AFTER INITIAL ‘B’ In above example all occurrences of ‘A’ after first occurrence of ‘B’ in source-string are counted and replaced by ‘#’. Tally-counter will be ‘3’ Source-string will be “AAB#bb#C#B”. INSPECT… with CONVERTING option This form converts characters from one sequence string to characters from equal size other sequence string on one to one mapping basis. These two sequence strings must be of same size. Example: Source-string = “AABAbbACAB” 1. INSPECT source-string CONVERTING ‘BXCY’ TO ‘1234’ In above example converts the characters B, X, C, Y to 1, 2, 3, and 4 respectively. Input : “AABAbbACAB” Output: “AA1AbbA3A1” Equivalent INSPECT with REPLACING option INSPECT source-string REPLACING ALL ‘B’ BY ‘1’ ALL ‘X’ BY ‘2’ ALL ‘C’ BY ‘3’ ALL ‘Y’ BY ‘4’ Difference between CONVERTING and REPLACING options in INSPECT Source-string = “AABAbbACAB” INSPECT source-string CONVERTING ‘AB’ TO ‘12’ Output: - “1121bb1C12” INSPECT source-string REPLACING ‘AB’ TO ‘12’ Output:- “A12AbbAC12” In example 1, all occurrences of characters A and B are replaced by characters 1 and 2 respectively. In example2, all occurrences of string ‘AB’ is replaced by string ‘12’. ```

NEXT CHAPTER TOPIC : COBOL

Visit COBOL books section in this site for good books