View previous topic :: View next topic |
Author |
Message |
gildan2020 Beginner
Joined: 07 Dec 2006 Posts: 17 Topics: 8
|
Posted: Tue Sep 18, 2007 5:51 am Post subject: Manipulate a string |
|
|
Hi guys,
I have a character field that looks like this:
1x3xx2xxxx
Take note that the numbers above are randomly placed within the field. Their positions might change. (The numbers are also random)
How do I check the position of "2" in the string above?
I'm trying to extract the string into as follows:
1x3xx2
Thanks. |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12382 Topics: 75 Location: San Jose
|
Posted: Tue Sep 18, 2007 6:23 am Post subject: |
|
|
gildan2020,
1. Click on the "Quick manuals" link on top of this page
2. Click on "Enterprise COBOL Language Reference" which is the first link on the right
3. Click
4. In the search box type UNSTRING and press Search
5. Read up the documentation for UNSTRING
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
jsharon1248 Intermediate
Joined: 08 Aug 2007 Posts: 291 Topics: 2 Location: Chicago
|
Posted: Tue Sep 18, 2007 9:01 am Post subject: |
|
|
UNSTRING writes the delimiter to a separate field. To get the delimiter in the result field, you'll need to use 2 steps. UNSTRING to split the text, and then STRING to add back the delimiter. When working with UNSTRING and STRING, don't forget to initialize variables used by the commands.
Code: |
01 WS-WORK-AREA.
05 PTR1 PIC S9(4) COMP VALUE +0.
05 PTR2 PIC S9(4) COMP VALUE +0.
05 CTR1 PIC S9(4) COMP VALUE +0.
05 WS-IN-TEXT PIC X(10).
05 WS-RESULT-TEXT PIC X(10).
05 WS-DELIM PIC X(10).
MOVE '1x3xx2xxxx' TO WS-IN-TEXT
MOVE SPACES TO WS-RESULT-TEXT WS-DELIM
UNSTRING
WS-IN-TEXT DELIMITED BY '2'
INTO WS-RESULT-TEXT DELIMITER IN WS-DELIM COUNT IN CTR1
END-UNSTRING
DISPLAY '***UNSTRING RESULTS***'
DISPLAY ' CTR1: ' CTR1
DISPLAY 'WS-RESULT-TEXT: ' WS-RESULT-TEXT
DISPLAY ' WS-DELIM: ' WS-DELIM
COMPUTE PTR1 = CTR1 + 1
STRING
WS-DELIM DELIMITED BY SPACE
INTO WS-RESULT-TEXT
WITH POINTER PTR1
END-STRING
DISPLAY '***STRING RESULTS***'
DISPLAY ' PTR1: ' PTR1
DISPLAY 'WS-RESULT-TEXT: ' WS-RESULT-TEXT
***UNSTRING RESULTS***
CTR1: 00005
WS-RESULT-TEXT: 1x3xx
WS-DELIM: 2
***STRING RESULTS***
PTR1: 00007
WS-RESULT-TEXT: 1x3xx2
|
If you're working with a single character delimiter, you might be better off moving the text to a table defined with PIC X(01) entries and then loop through the characters moving them 1 by 1 to your result field. "half a dozen of one, six of another..." |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12382 Topics: 75 Location: San Jose
|
Posted: Tue Sep 18, 2007 9:13 am Post subject: |
|
|
Quote: |
UNSTRING to split the text, and then STRING to add back the delimiter.
|
jsharon1248,
You don't need to string command , a simple move will do
Code: |
01 WS-STR PIC X(80) VALUE '1X3XX2XXXX '.
01 WS-PTR PIC S9(04) COMP.
01 WS-FLD PIC X(80).
MOVE +1 TO WS-PTR
UNSTRING WS-STR DELIMITED BY '2' INTO WS-FLD
WITH POINTER WS-PTR
MOVE WS-STR (WS-PTR - 1 : 1) TO WS-FLD(WS-PTR - 1 : 1)
|
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
jsharon1248 Intermediate
Joined: 08 Aug 2007 Posts: 291 Topics: 2 Location: Chicago
|
Posted: Tue Sep 18, 2007 9:30 am Post subject: |
|
|
Kolusu
True. If the actual requirements utilize a single character delimiter, the MOVE will suffice. I was anticipating that the actual requirements might utilize a multi-character delimiter of varying length and used the STRING to handle that. Looking at the code I posted, I'd need to add the ALL keyword to the DELIMITED BY option. |
|
Back to top |
|
 |
gildan2020 Beginner
Joined: 07 Dec 2006 Posts: 17 Topics: 8
|
Posted: Wed Sep 19, 2007 12:45 am Post subject: |
|
|
Hi guys,
Thanks for all ur replies.
However, there's a slight problem. The number "2" is just an example. In reality, the last digit can be random (ranging from 1 to 9).
I'm not sure if I can use the "DELIMITED BY" like this. Any ideas? |
|
Back to top |
|
 |
vivek1983 Intermediate

Joined: 20 Apr 2006 Posts: 222 Topics: 24
|
Posted: Wed Sep 19, 2007 1:35 am Post subject: |
|
|
gildan2020,
Can you provide the outputs for the following inputs? Also check if the following are valid inputs.
1. 1xxxxx2xx9
2. 1x59xxx5
3. 1xxxxxxxx3x4
4. 1xyaa4gggx3 _________________ Vivek G
--------------------------------------
A dream is just a dream. A goal is a dream with a plan and a deadline. (Harvey Mackay) |
|
Back to top |
|
 |
jsharon1248 Intermediate
Joined: 08 Aug 2007 Posts: 291 Topics: 2 Location: Chicago
|
Posted: Wed Sep 19, 2007 8:44 am Post subject: |
|
|
The UNSTRING syntax allows you to specify a variable or a literal in the DELIMITED BY clause. For simplicity, I just used the literal in the example I posted. Just define an additional variable for the delimiter, populate it with whatever you need, and reference that in the UNSTRING. I don't know if you need it, but you can also specify multiple delimiters by using OR in the DELIMITED BY clause.
Code: |
05 WS-SEARCH-DELIM PIC X(01).
MOVE '1x3xx2xxxx' TO WS-IN-TEXT
MOVE SPACES TO WS-RESULT-TEXT WS-DELIM
MOVE '2' TO WS-SEARCH-DELIM
UNSTRING
WS-IN-TEXT DELIMITED BY WS-SEARCH-DELIM
INTO WS-RESULT-TEXT DELIMITER IN WS-DELIM COUNT IN CTR1
END-UNSTRING
|
|
|
Back to top |
|
 |
gildan2020 Beginner
Joined: 07 Dec 2006 Posts: 17 Topics: 8
|
Posted: Wed Sep 19, 2007 9:30 am Post subject: |
|
|
vivek1983 wrote: | gildan2020,
Can you provide the outputs for the following inputs? Also check if the following are valid inputs.
1. 1xxxxx2xx9
2. 1x59xxx5
3. 1xxxxxxxx3x4
4. 1xyaa4gggx3 |
Hi,
Assuming I need the first five chars only
Code: | 1. 1xxxxx2xx9 --> 1xxxx|x2xx9
2. 1x59xxx5 --> 1x59x|xx5
3. 1xxxxxxxx3x4 --> 1xxxx|xxxx3x4
4. 1xyaa4gggx3 --> 1xyaa|4gggx3 |
Therefore:
Code: | 1. 1 trailing Xs are to be discarded
2. 1x59 trailing Xs are to be discarded
3. 1 trailing Xs are to be discarded
4. 1xyaa |
|
|
Back to top |
|
 |
jsharon1248 Intermediate
Joined: 08 Aug 2007 Posts: 291 Topics: 2 Location: Chicago
|
Posted: Wed Sep 19, 2007 10:19 am Post subject: |
|
|
gildan2020
You're only giving us little bits and pieces of what you're trying to do. First you told us you're trying to split a field based on a varying delimiter, but your last post looks like you're trying to eliminate trailing characters after splitting out the first 5 characters. Help us out. Give us a full description of the requirements and several examples of the input and results. |
|
Back to top |
|
 |
|
|