IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

UNSTRING command in COBOL


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Sweta Saraogi

New User


Joined: 07 Feb 2007
Posts: 5
Location: India

PostPosted: Tue Jul 01, 2008 3:31 pm
Reply with quote

UNSTRING in COBOL

Explain about the syntax and logic of UNSTRING verb in COBOL with examples.
Back to top
View user's profile Send private message
Rajesh Sampath

New User


Joined: 18 Jun 2008
Posts: 9
Location: Pune

PostPosted: Tue Jul 01, 2008 6:26 pm
Reply with quote

Syntax from manual:

The UNSTRING statement causes contiguous data in a sending field to be separated and placed into multiple receiving fields.


Code:

>>-UNSTRING--identifier-1--------------------------------------->

>--+-------------------------------------------------------------------------------------+-->
   '-DELIMITED--+----+--+-----+--+-identifier-2-+--+-----------------------------------+-'   
                '-BY-'  '-ALL-'  '-literal-1----'  | .-------------------------------. |     
                                                   | V                               | |     
                                                   '---OR--+-----+--+-identifier-3-+-+-'     
                                                           '-ALL-'  '-literal-2----'         

>--INTO--------------------------------------------------------->

   .------------------------------------------------------------------------------------.   
   V                                                                                    |   
>----identifier-4--+---------------------------------+--+-----------------------------+-+-->
                   '-DELIMITER--+----+--identifier-5-'  '-COUNT--+----+--identifier-6-'     
                                '-IN-'                           '-IN-'                     

>--+---------------------------------+-------------------------->
   '-+------+--POINTER--identifier-7-'   
     '-WITH-'                           

>--+--------------------------------+--------------------------->
   '-TALLYING--+----+--identifier-8-'   
               '-IN-'                   

>--+------------------------------------------+----------------->
   '-+----+--OVERFLOW--imperative-statement-1-'   
     '-ON-'                                       

>--+-----------------------------------------------+------------>
   '-NOT--+----+--OVERFLOW--imperative-statement-2-'   
          '-ON-'                                       

>--+--------------+--------------------------------------------><
   '-END-UNSTRING-'   


The UNSTRING statement allows you to take the data in one field and break it out into several fields. With the UNSTRING, again only the characters that precede the character(s) in the DELIMITED clause are moved to the fields specified in the INTO. Looking at the layout for the verb, you can see it has several options. The ALL clause means that two or more of the character specified as a delimiter will be treated as if there was only one and none of the delimiter characters will be moved to the INTO/receiving fields. Notice that you can have an OR so that you can check to see if the delimiter is one thing or another. If you want to keep the delimiter you can use the DELIMITER in clause to receive the character that was found and used as the delimiter.

There is also a COUNT IN option that allows the programmer to count the number of characters that get moved into the INTO/receiving field. You can specify a different count field for each of the receiving fields. The WITH POINTER option can designate a position in the sending field that will be the first character that is sent. This means if the pointer is 5, the first four characters will not be sent. Every time a character is sent to the receiving field, the POINTER is incremented by 1. The TALLYING IN option counts how many of the receiving fields actually used. This usually means received data but if there were two delimiters next to each other and you didn't use the all you could conceivably end up with an empty field that would be counted as used. The ON OVERFLOW will be used when all of the data cannot be unstrung successfully or the pointer is out of range.
Back to top
View user's profile Send private message
UmeySan

Active Member


Joined: 22 Aug 2006
Posts: 771
Location: Germany

PostPosted: Tue Jul 01, 2008 9:08 pm
Reply with quote

Hi Sweta Saraogi !


How the UNSTRING works

The UNSTRING copies characters from the source string, to the destination string, until a condition is encountered that terminates data movement.

When data movement ends for a particular destination string, the next destination string becomes the receiving area and characters are copied into it until once again a terminating condition is encountered.

Characters are copied from the source string to the destination strings according to the rules for alphanumeric moves. There is space filling.

Data movement termination

When the DELIMITED BY clause is used data movement from the source string to the current destination string ends when either ;

a delimiter is encountered in the source string
the end of the source string is reached.
When the DELIMITED BY clause is not used, data movement from the source string to the current destination string ends when either;

the destination string is full
the end of the source string is reached

UNSTRING termination

The UNSTRING statement terminates when either;

All the characters in the source string have been examined
All the destination strings have been processed
Some error condition is encountered (such as the pointer pointing outside the source string).

UNSTRING rules

Where a literal can be used any figurative constant can be used except the ALL (literal).

When a figurative constant is used then its length is one character.

Characters are moved from the source string to the destination strings according to the rules for the MOVE, with space filling if required.

The delimiter is moved into HoldDelim$i according to the rules for the MOVE.

The DELIMITER IN and COUNT IN phrases may be specified only if the DELIMITED BY phrase is used.


Here is an example for UNSTRING:

Code:

IDENTIFICATION DIVISION.
PROGRAM-ID. UNSTRING1.
DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-STRING PIC A(30) VALUE 'DEMO TO UNSTRING'.
   01 WS-STR1 PIC A(4).
   01 WS-STR2 PIC A(2).
   01 WS-STR3 PIC A(8).
   01 WS-COUNT PIC 99 VALUE 1.
PROCEDURE DIVISION.
   UNSTRING WS-STRING DELIMITED BY SPACE
      INTO WS-STR1, WS-STR2, WS-STR3
   END-UNSTRING.
   DISPLAY 'WS-STR1 : 'WS-STR1.
   DISPLAY 'WS-STR2 : 'WS-STR2.
   DISPLAY 'WS-STR3 : 'WS-STR3.
   STOP RUN.


When you compile and execute the above program, it produces the following result:

WS-STR1 : DEMO
WS-STR2 : TO
WS-STR3 : UNSTRING
Back to top
View user's profile Send private message
dp33770

New User


Joined: 04 Jul 2007
Posts: 91
Location: Hyderabad

PostPosted: Tue Jan 20, 2009 8:52 pm
Reply with quote

I also need to UNSTRING a string into 3 different fields.

Example Input Strings
--------------------
Code:
1.AAAAAA, BB 123456
2.CCCCCC,BB 123456
3.EEEEEE, BB 123456



Rule
----
1.The first string may be till I find a comma(,)
2.The second string will be after comma but shuld not contain any leading and trailing Spaces.
3.The third string will be remaining characters.


Can anyone help me in this
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Tue Jan 20, 2009 9:10 pm
Reply with quote

The UNSTRING statement to split one sending field into several receiving fields. One UNSTRING statement can save you several MOVE statements.

You can indicate delimiters that, when encountered in the sending field, cause the current receiving field to be switched to the next one indicated. You might have the number of characters placed in each receiving field returned to you, and keep a count of the total number of characters transferred. You might also specify special actions for the program to take if all the receiving fields are filled before the end of the sending item is reached.
Quote:

Can anyone help me in this

do all input strings have 2 commas?

if so:
Code:

UNSTRING INPUT-STRING
                   DELIMITED BY ','
    INTO FIELD-1
         TEMP-FIELD-2
         FIELD-3
END-UNSTRING.

IF TEMP-FIELD-2(1:1) = SPACE
THEN
    UNSTRING TEMP-FIELD-2
                   DELIMITED BY ALL SPACES
        INTO TEMP-FIELD-1
             FIELD-2
             TEMP-FIELD-3
    END-UNSTRING
ELSE
    MOVE TEMP-FIELD-2   TO FIELD-2
END-IF.
Back to top
View user's profile Send private message
dp33770

New User


Joined: 04 Jul 2007
Posts: 91
Location: Hyderabad

PostPosted: Tue Jan 20, 2009 11:22 pm
Reply with quote

Hello Dick,

Quote:
do all input strings have 2 commas ?

My input string have only 1 comma .
For ex For input string 1 i,e

AAAAAA, BB 123456 i need three o/p strings as below
a. AAAAAA
b. BB
c. 123456

Note: after the comma there may be one or two spaces.

pls suggest..
Back to top
View user's profile Send private message
Cristopher

New User


Joined: 31 Jul 2008
Posts: 53
Location: NY

PostPosted: Wed Jan 21, 2009 3:45 pm
Reply with quote

dp33770,
What do u think the PIC definition of field second should be in the light that you dont want leading or trailing spaces?
For the given case:
Quote:
AAAAAA, BB 123456 i need three o/p strings as below
a. AAAAAA
b. BB
c. 123456

It should be PIC X(2) ....it can be 3 ...4.... and so on.So, You wish to dynamically allocate the length to your variable.
You can certainly achieve that by using occurs with depending on clause.

Please confirm my understanding.
Cris
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Wed Jan 21, 2009 4:54 pm
Reply with quote

unstring the AAAAAAA and the BB 123456 delimited by comma.

unstring the BB 123456 delimited by all spaces.
Back to top
View user's profile Send private message
Cristopher

New User


Joined: 31 Jul 2008
Posts: 53
Location: NY

PostPosted: Wed Jan 21, 2009 5:45 pm
Reply with quote

There can be multiple scenarios depending on the Input and the expected rules. For the following input :
1.AAAAAA, BB 123456
2.CCCCCC,BB 123456
3.EEEEEE, BB 123456
The following code should work fine:
Code:
UNSTRING F  DELIMITED BY ','         
    INTO F1                           
         F2     
UNSTRING F2 DELIMITED BY ALL SPACES   
    INTO F3                           
         F4                           
         F5

If F3 = Spaces then F4 and F5 are the fields you would be interested in.
Moreover if you have data after 'CCCCCC,BB 123456' e.g. 'CCCCCC,BB 123456 78' ...78 wont be captured.

Cris
Back to top
View user's profile Send private message
cnu_shalini

New User


Joined: 03 Aug 2007
Posts: 1
Location: chennai

PostPosted: Wed Feb 17, 2010 2:56 pm
Reply with quote

Hi All,

I have a problem here. I have variable A with value "KONDURU SRINIVASULU RAJU".

I want to unstring this variable in such a way that my first name should be "KONDURU" and my last name should be "SRINIVASULU RAJU".

Could anybody help me in this?

Thanks in advance.
Back to top
View user's profile Send private message
Mathiv Anan

Active User


Joined: 23 Jul 2008
Posts: 106
Location: USA

PostPosted: Wed Feb 17, 2010 3:50 pm
Reply with quote

You can achieve this using an INSPECT to introduce a delimiter in first occurrence of space in the variable and then UNSTRING on it.

Code:
INSPECT WS-NAME REPLACING FIRST SPACE BY ':'.
DISPLAY 'NAME:' WS-NAME

UNSTRING WS-NAME DELIMITED BY ':'
         INTO WS-F1,
                 WS-F2

DISPLAY 'WS-F1' WS-F1
DISPLAY 'WS-F2' WS-F2


If WS-NAME has your full name,
WS-F1 will have your first name
WS-F2 will have your expected last name.
Back to top
View user's profile Send private message
UmeySan

Active Member


Joined: 22 Aug 2006
Posts: 771
Location: Germany

PostPosted: Thu Feb 18, 2010 1:43 pm
Reply with quote

@ cnu_shalini

Morning Sir !

You also could achieve this by using UNSTRING with POINTERS.
You could also apply COUNT IN parameter to get the length of the strings.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts RACF - Rebuild SETROPTS command which... All Other Mainframe Topics 3
No new posts Replace each space in cobol string wi... COBOL Programming 2
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
No new posts Routing command Address SDSF to other... TSO/ISPF 2
Search our Forums:

Back to Top