COBOL Unstring

COBOL String Statement COBOL Move Statement

Advertisements


The UNSTRING statement separates the single data item into two or more data items.

UNSTRING statement does the task of several MOVE statements job.

UNSTRING used to separate single string to multiple based on the delimiter provided in the UNSTRING.


Syntax:


UNSTRING {identifier/literal} [DELIMITED BY SIZE/SPACE/specified delimiter] 

   INTO {identifier-1/literal-1} [DELIMITER IN delimete-1][COUNT IN counter-1] 
	{identifier-2/literal-2} [DELIMITER IN delimete-2][COUNT IN counter-2] 
		.
		.
	{identifier-N/literal-N} [DELIMITER IN delimete-N][COUNT IN counter-N] 
	[WITH POINTER ws-pointer]
	[TALLYING ON identifier-t]
	[ON OVERFLOW imperative-statements-1]
	[NOT ON OVERFLOW imperative-statements-2]
[END-UNSTRING.]


UNSTRING requires minimum two destination identifiers/literals are required to separate.

UNSTRING statement can separate alphabetic and alpha-numeric items.

UNSTRING statement can’t separate on numeric and floating point items.

UNSTRING allows only SPACE which is figurative constants.

UNSTRING will not add any single character to the target field.

DELIMITED BY, TALLYING, WITH POINTER, ON OVERFLOW and NOT ON OVERFLOW clauses is optional in UNSTRING usage.

END-UNSTRING is optional during the UNSTRING statement usage.

END-UNSTRING used as a scope terminator for UNSTRING statement.


DELIMITED BY:

DELIMITED BY phase used to control the data transfers.

DELIMITED BY phase is optional in UNSTRING statement.

When DELIMITED BY phase not specified, DELIMITED IN phase should not be specified.

When DELIMITED BY phase not specified, CONUNT IN phase should not be specified.

The delimiter can be two or more characters but the characters should be contagious or must be in sequence.


WITH POINTER:

POINTER option is used to points the next character of delimiter item in the source data item.

POINTER option is optional in UNSTRING statement.

If we specify the POINTER option, then we need to provide a value which is greater than ZERO or less than the length of data item.


COUNT IN:

COUNT IN clause associated with particular destination data item.

COUNT IN holds the count of no of character passed to the particular destination identifier/data item.

COUNT IN option is optional with UNSTRING statement.


TALLYING:

TALLYING clause holds the count of destination strings affected by UNSTRING.

TALLYING clause associated with all the destination data items.

TALLYING clause is optional with UNSTRING statement.


ON OVERFLOW:

ON OVERFLOW is used to execute a set of imperative statements when overflow occurs during UNSTRING separation.

ON OVERFLOW will trigger the imperative statements based on the implicit or explicit pointer value.

OVERFLOW condition occurred; no more data will be transferred to destination fields.

If OVERFLOW occurred, UNSTRING operation will be terminated.

ON OVERFLOW is optional in UNSTRING statement.


NOT ON OVERFLOW:

NOT ON OVERFLOW is used to execute the set of imperative statements when the UNSTRING is successful.

NOT ON OVERFLOW is fully reverse to ON OVERFLOW functionality.


Practical Example:


Below program shows the UNSTRING coding in the COBOL program.


Code:


IDENTIFICATION DIVISION.                                       
PROGRAM-ID. UNSTRFUN.                                           
ENVIRONMENT DIVISION.                                           
DATA DIVISION.                                                  
WORKING-STORAGE SECTION.                                        
01 WS-DATA		PIC X(70)                              
VALUE ‘MAINFRAMESTECHHELP, IS A MAINFRAME COMMUNITY'.          
01 WS-OUTPUT-DATA1       PIC X(20).                             
01 WS-OUTPUT-DATA2       PIC X(50).                             
PROCEDURE DIVISION.                                             
    DISPLAY 'UNSTRING EXAMPLE....'.                             
    UNSTRING WS-DATA DELIMITED BY ","                          
      INTO  WS-OUTPUT-DATA1,  WS-OUTPUT-DATA2                   
      ON OVERFLOW DISPLAY "ERROR OCCURED"                       
      NOT ON OVERFLOW DISPLAY "NO ERROR".                       
    DISPLAY 'DATA1 AFTER UNSTRING FUNCTION : ' WS-OUTPUT-DATA1. 
    DISPLAY 'DATA2 AFTER UNSTRING FUNCTION : ' WS-OUTPUT-DATA1. 
    STOP RUN.                                                   



COBOL String Statement COBOL Move Statement

Advertisements