Saturday, 10 August 2013

Nail COBOL UNSTRING statement with UNSTRING in COBOL with example. [Unstring in COBOL].

COBOL UNSTRING, unstring cobol, cobol unstring examples
COBOL UNSTRING Statement.

Welcome back to today's session on UNSTRING in COBOL. In this session, you'll learn how to unstring string in COBOL by using COBOL UNSTRING Statement. COBOL UNSTRING Tutorial begins with an introduction to unstring statement in COBOL. Followed by COBOL unstring example and COBOL unstring statement syntax. Let's get started with COBOL unstring statement.  

COBOL UNSTRING Tutorial - Agenda.
  • Introduction. 
  • What is UNSTRING in COBOL?
  • COBOL UNSTRING Syntax. 
  • COBOL UNSTRING Example.
  • Conclusion.  

Introduction. 

String manipulation is an integral part of any programing language. In COBOL, you generally used COBOL STRING, COBOL UNSTRING statement, and COBOL Inspect statement to perform string operations such as splitting string or concatenating string.

What is COBOL UNSTRING Statement [Unstring in COBOL]?

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 Syntax.


UNSTRING in COBOL, cobol unstring command, string syntax in cobol

COBOL UNSTRING

If you look above COBOL unstring syntax, then you notice that the operation of UNSTRING is modified by several 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.

Let's try to understand the COBOL unstring statement with the help of an example. In this unstring COBOL example, the employee name is split into first name, last name, and middle name. 

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

Note: The NAME-IN field is entered as "TAFT, WILLIAM, H". NAME-OUT will appear as follows after the COBOL UNSTRING operations:
UNSTRING IN COBOL, unstring COBOL example, COBOL unstring example
COBOL UNSTRING

Important Point: 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.

When to use UNSTRING Statement in COBOL?

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 COBOL 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 COBOL UNSTRING can do.

#1. COBOL UNSTRING Example. 

The first COBOL Unstring 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.

 

#2. COBOL UNSTRING into Array Example.

The second COBOL unstring example use unstring statement to 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

#3. COBOL UNSTRING Example.

The third COBOL Unstring 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.

*
WORKING STORAGE SECTION.
10 WS-DELIM PIC X(01) VALUE X'09'.
.
.
*
PROCEDURE DIVISION.
A00-SPLIT.
UNSTRING CUSTOMER-NAME DELIMITED BY WS-DELIM
INTO F-NME, L-NME
END-UNSTRING

#4. COBOL UNSTRING DELIMITED BY Example.

The final COBOL unstring 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.

COBOL UNSTRING - GENERAL RULES.

  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.

UNSTRING in COBOL - Interview Question and Answers.

  • Q. When using the String statement, is the target field cleared before the String operation is performed?
  • A. 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.
  • Q. Can I string more than two fields together in a single String statement?
  • A. Yes. You can list the different fields you want to use in a single String statement.
  • Q. What if I want to use a different delimiter for each field?
  • A. 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.
  • Q. Must the delimiters always be single characters? Can I use something like “SEPARATOR“ as a delimiter?
  • A. Delimiters can be of any size that can be contained in the source field. The word “SEPARATOR” can be used as a delimiter.
  • Q. How do I find out how many fields are found when I unstring a field? I don’t know how many to expect.
  • A. 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.
  • Q. 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?
  • A. 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.

COBOL UNSTRING Statement - Youtube video. 

    Summary.

    Finally, this mark an end to today's COBOL UNSTRING Tutorial. 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.


    ►Subscribe to Topictrick & Don't forget to press THE BELL ICON to never miss any updates. Also, Please visit mention the link below to stay connected with Topictrick and the Mainframe forum on - 

    ► Youtube
    ► Facebook 
    ► Reddit

    Thank you for your support. 
    Mainframe Forum™

    No comments:

    Post a comment