Saturday, 10 August 2013

COBOL UNSTRING | UNSTRING in COBOL with example.

COBOL Program
COBOL UNSTRING

In today’s tutorial, you’ll learn COBOL UNSTRING. It’s largely used for string manipulation in COBOL programs. You’ll also learn the various aspects of UNSTRING in COBOL with example.

What is COBOL UNSTRING?


In laymen's terms, the COBOL UNSTRING statement is used to decouple or unstring into two or more fields from single fields. Here, you use the Delimited By clause to specify the delimiter that marks the end of each field that is to be unstrung. 

UNSTRING statement in COBOL can be used in conjunction with reference modification in COBOL. With the UNSTRING, the programmer can use POINTER and ON OVERFLOW. NOT ON OVERFLOW as well as an END-UNSTRING scope terminator can also be used.

COBOL UNSTING FORMAT.

UNSTRING
COBOL UNSTRING

As you can see by examining the metalanguage, the operation of UNSTRING is modified by a number of clauses. These clauses affect the operation of UNSTRING as follows:

  • DELIMITED BY: When the DELIMITED BY clause is used, characters are examined in the source string and copied to the current destination string until one of the specified delimiters is encountered in the source string or the end the source string is reached. 
  • ON OVERFLOW: When ON OVERFLOW activates, the statement block following it is executed. 
  • COUNT IN: The COUNT IN clause is associated with a particular destination string and holds a count of the number of characters passed to the destination string, regardless of whether they were truncated.
  • TALLYING IN: Only one TALLYING clause can be used with each UNSTRING. It holds a count of the number of destination strings affected by the UNSTRING operation.
  • WITH POINTER: When the WITH POINTER clause is used, the Pointer data item holds the position of the next non-delimiter character to be examined in the source string. 
  • ALL: When the ALL phrase is used, contiguous delimiters are treated as if only one delimiter had been encountered. If ALL is not used, contiguous delimiters result in spaces being sent to some of the destination strings.

COBOL UNSTRING EXAMPLE.

UNSTRING NAME-OUT DELIMITED BY ','
    INTO LAST-NAME
         FIRST-NAME
         MIDDLE-INITIAL

Suppose NAME-IN is entered as "TAFT, WILLIAM, H". NAME-OUT will appear as follows after the UNSTRING:
UNSTRING IN COBOL
COBOL UNSTRING

Note: With the UNSTRING, the programmer can use POINTER and ON OVERFLOW. NOT ON OVERFLOW as well as an END-UNSTRING scope terminator can also be used.

How to use the COBOL UNSTRING Statement?


When data is entered interactively via an online screen, you sometimes need to convert it into a more concise form for processing purposes. Similarly, when data is to be displayed on the online screen or on reports, you need to convert it from a concise form to a more readable form. So, you can use the STRING and UNSTRING statements.
String splitting involves chopping a string into a number of smaller strings. In the COBOL program, string splitting is done using the UNSTRING verb. Let’s look at some examples to see what UNSTRING can do.
The first example uses UNSTRING to break a customer name into its three constituent parts: first name, middle name, and surname. For example, the string “MAINFRAME FORUM" is broken into the two strings “MAINFRAME” and “FORUM”:

UNSTRING BLOG-NAME DELIMITED BY ALL SPACES
INTO FRST-NAME, LST-NAME
END-UNSTRING.
The second example breaks an address string (where the parts of the address are separated from one another by commas) into separate address lines. The address lines are stored in a six-element table. Not all addresses have six parts exactly, but you can use the TALLYING clause to discover how many parts there are:
UNSTRING CUST-ADDRESS DELIMITED BY ";"
INTO ADRLN1(1), ADRLN2(2), ADRLN3(3),
ADRLN4(4), ADRLN5(5)
TALLYING IN ADRLNCNT
END-UNSTRING
The third example is to split the string into separate variables whenever TAB appears in the middle of the sting by using UNSTRING DELIMITED by TAB in COBOL. In such a scenario, you can pass HEX value i.e. X'09' of TAB.

10 WS-DELIM PIC X(01) VALUE X'09'.
.
.
UNSTRING CUSTOMER-NAME
DELIMITED BY WS-DELIM INTO F-NME L-NME
END-UNSTRING

The final example breaks a simple comma-delimited record into its constituent parts. Because the fields are not fixed length, they need to be validated for length—and that requires finding out how long each field is. The COUNT IN clause, which counts the number of characters transferred to a particular destination field, is used to determine the actual length of the field:
UNSTRING SUPPRec DELIMITED BY ","
INTO SUPP-CDE COUNT IN CNTR1
SUPP-NME COUNT IN CNTR2
SUPP-ADD COUNT IN CNTR3
END-UNSTRING

GENERAL RULES FOR USING THE UNSTRING

  1. The sending field must be non-numeric. The receiving fields may be numeric or non-numeric.
  2. Each literal must be non-numeric.
  3. The [WITH POINTER identifier] and [ON OVERFLOW imperative-statement] clauses may be used in the same way as with the STRING.

Interview Question and Answers.

  • When using the String statement, is the target field cleared before the String operation is performed?
  • No, so that you can execute multiple string operations into a single target. The Pointer clause allows you to position the next character in the target field.
  • Can I string more than two fields together in a single String statement?
  • Yes. You can list the different fields you want to use in a single String statement.
  • What if I want to use a different delimiter for each field?
  • You can list the different delimiters with each field you are unstringing. If all the fields use the same delimiter, you only need to specify the Delimited By clause once, after all the fields are listed.
  • Must the delimiters always be single characters? Can I use something like “SEPARATOR“ as a delimiter?
  • Delimiters can be of any size that can be contained in the source field. The word “SEPARATOR” can be used as a delimiter.
  • How do I find out how many fields are found when I unstring a field? I don’t know how many to expect.
  • You can determine the number of target fields used by an Unstring operation by specifying the Tallying In clause on your Unstring statement. The tally field is incremented by the number of fields changed. Be careful that you initialize the tally field each time it is used, as the Unstring statement does not automatically do this for you.
  • I already used Unstring to operate on part of a field. I want to Unstring some more data, but I don’t want to start over at the beginning of the field. I know that reference modification is not allowed. What should I do?
  • You may use the Pointer clause of the Unstring statement to indicate a data field containing the position of the next character that should be included in the Unstring operation.

Summary.

This tutorial introduced COBOL string manipulation. You discovered how to use UNSTRING to count, convert, and replace characters in a string. You saw how to use the UNSTRING in COBOL PROGRAM to split a string into substrings. In addition to learning the basics of the string-handling statements, you saw how to leverage their capabilities by using reference modification.

No comments:

Post a Comment