Saturday, January 31, 2009

String Manipulation in COBOL

A string refers to a sequence of characters. String manipulation operations include finding a particular character/sub-string in a string, replacing particular character/sub-string in a string, concatenating strings and segmenting strings. All these functions are handled by three verbs INSPECT, STRING and UNSTRING in COBOL. EXAMINE is the obsolete version of INSPECT supported in COBOL74.

INSPECT
  • INSPECT- FOR TALLYING - It is used to tally the occurrence of a single character or groups of characters in a data field.
    Syntax:
    INSPECT identifier-1 TALLYING identifier-2 FOR ALL/LEADING literal-1|identifier-3 [BEFORE|AFTER INITIAL identifier-4|literal-2] - Optional.
    INSPECT identifier-1 TALLYING identifier-2 FOR CHARACTERS [BEFORE|AFTER INITIAL identifier-4|literal-2] - Optional.
    Main String is identifier-1 and count is stored in identifier-2. Literal-1 or Identifier-3 is a character or group-of-characters you are looking in the main-string. INSPECT further qualifies the search with BEFORE and AFTER of the initial occurrence of identifier-4 or literal-2.
    Example:
    WS-NAME - 'HELLO HOW ARE YOU DOING?'
    INSPECT WS-NAME TALLYING WS-COUNT ALL 'O'
    BEFORE INITIAL 'HOW' AFTER INITIAL 'YOU'
    END-INSPECT
    Result:
    WS-COUNT contains - 2
  • INSPECT- FOR REPLACING : It is used to replace the occurrence of a single character or groups of characters in a data field.
    Syntax:
    INSPECT identifier-1 REPLACING ALL|LEADING literal-1|identifier-2 BY identifier-3|literal-2 [BEFORE|AFTER INITIAL identifier-4|literal-2] - Optional.
    INSPECT identifier-1 REPLACING CHARACTERS BY identifier-2 BEFORE|AFTER INITIAL identifier-3|literal-1
  • INSPECT-FOR COUNTING AND REPLACING It is a combination of the above two methods. INSPECT identifier-1 TALLYING (tallying part ) REPLACING (replacing part)
STRING

STRING command is used to concatenate one or more strings.

Syntax:
STRING identifier-1 / literal-1, identifier-2/ literal-2 DELIMITED BY (identifier-3/literal-3/SIZE) INTO identifier-4 END-STRING.
01 VAR1 PIC X(10) VALUE 'MUTHU '
01 VAR2 PIC X(10) VALUE 'SARA '
01 VAR2 PIC X(20).
To get display 'MUTHU,SARA'
STRING VAR1 DELIMITED BY ' ' ',' DELIMITED BY SIZE VAR2 DELIMITED BY ' ' INTO VAR3 END-STRING.
The receiving field must be an elementary data item with no editing symbols and JUST RIGHT clause.
With STRING statement, specific characters of a string can be replaced whereas MOVE replaces the full string.
01 AGE-OUT PIC X(12) VALUE '12 YEARS OLD'.
STRING '18' DELIMITED BY SIZE INTO AGE-OUT. => 18 YEARS OLD.

Reference Modification - equivalent of SUBSTR

'Reference modification' is used to retrieve or overwrite a sub-string of a string. ':' is known as reference modification operator.

Syntax:
String(Starting-Position:Length)
MOVE '18' TO AGE-OUT(1:2) does the same as what we did with STRING command.
When it is used in array elements, the syntax is Array-element (occurrence) (Starting-Position:Length)

UNSTRING

UNSTRING command is used to split one string to many strings.

Syntax:

UNSTRING identifier-1 [DELIMITED BY (ALL/) identifier2/literal1 [,OR (ALL/) (identifier-3/literal-2),..]] INTO identifier-4 [,DELIMITER IN identifier-5, COUNT IN identifier-6] [,identifier-7 [,DELIMITER IN identifier-8, COUNT IN identifier-9]
01 WS-DATA PIC X(12) VALUE '10/200/300/1'.
UNSTRING WS-DATA DELIMITED BY '/'
INTO WS-FLD1 DELIMITER IN WS-D1 COUNT IN WS-C1
WS-FLD2 DELIMITER IN WS-D2 COUNT IN WS-C2
WS-FLD3 DELIMITER IN WS-D3 COUNT IN WS-C3
END-UNSTRING.
Result:
WS-FLD1 = 10 WS-FLD2 =200 WS-FLD3=300 WS-C1 = 2 WS-C2=3 WS-C3=3 WS-D1 = '/' WS-D2='/' WS-D3 ='/'
ON OVERFLOW can be coded with STRING and UNSTRING. If there is STRING truncation then the imperative statements followed ON OVERFLOW will be executed.

Please do provide your comments or reactions. You may also post your questions in the comments.

1 comment:

Unknown said...

Good info. For some more samples on INSPECT,please refer
http://clearmainframeinterview.blogspot.com/2014/01/cobol-working-with-inspect.html

Sreenivas
http://clearmainframeinterview.blogspot.com