|
|
| Author |
Message |
rathinakarthik
New User
Joined: 25 Jul 2006 Posts: 22
|
|
|
|
Hi,
I have a string
Say Str1 = 'a,b,c,d,e'
I want to split this into two variables based on comma as separator. The values of two variables need to be
Var1 = 'a'
Var2 = 'b,c,d,e'
Again i have to take var2 and split based on comma into 'b' and 'c,d,e' and similarly it goes on.
I used UNSTRING to try this
UNSTRING Str1
DELIMITED BY ','
INTO VAR1, VAR2
END-UNSTRING
But the values i got are
Var1 = 'a'
Var2 = 'b'
I need value of var2 as 'b,c,d,e'
Can we prevent Unstring option from delimiting the receiving field?
I want to Delimit based in First occerenc alone
Is there any Option for this. Please advise
Thank you |
|
| Back to top |
|
 |
References
|
Posted: Thu Apr 24, 2008 6:16 pm Post subject: Re: UNSTRING the First occurence Alone |
 |
|
|
 |
Bharath Bhat
Active User
Joined: 20 Mar 2008 Posts: 67 Location: chennai
|
|
|
|
Hi,
Could you please post declarations of var1 and var2. |
|
| Back to top |
|
 |
rathinakarthik
New User
Joined: 25 Jul 2006 Posts: 22
|
|
|
|
Hi,
var1 will be pic x(1)
var2 will be pix x(20)
Thanks |
|
| Back to top |
|
 |
ashimer
Senior Member
Joined: 13 Feb 2004 Posts: 344 Location: Bangalore
|
|
|
|
try this way ...
var1 pic x(1)
vart pic x(1)
var2 pic x(20)
UNSTRING Str1
INTO VAR1, VART, VAR2
END-UNSTRING
after this stmt ull get a in var1 comma in vart and b,c,d,e in var2
now move var2 to str1 and do the following steps again for b,c,d,e
try and let us know ...
thanks
ashimer |
|
| Back to top |
|
 |
dick scherrer
Global Moderator
Joined: 23 Nov 2006 Posts: 7926 Location: 221 B Baker St
|
|
|
|
Hello,
| Quote: |
| Again i have to take var2 and split based on comma into 'b' and 'c,d,e' and similarly it goes on. |
Do you really need this or is the requirement to get 5 vars with the values a thru e (var1 = a, var2 = b, etc.)? What use is a variable with 'c,d,e' (other than to further break it down)?
A single unstring delimited by ',' naming 5 vars would break the entire field - which sounds like what your are coding to reach. |
|
| Back to top |
|
 |
dick scherrer
Global Moderator
Joined: 23 Nov 2006 Posts: 7926 Location: 221 B Baker St
|
|
|
|
Hello again,
If you really wanted to unstring the first comma-delimited value into a variable and all of the other values into a second variable you could use:
| Code: |
01 SOME-DATA.
05 STR1 PIC X(09) VALUE 'A,B,C,D,E'.
05 VAR1 PIC X(20).
05 VAR2 PIC X(20).
*
PROCEDURE DIVISION.
000-STUFF.
DISPLAY 'STR1 = ' STR1.
INSPECT STR1 REPLACING FIRST ',' BY '\'
UNSTRING STR1 DELIMITED BY '\'
INTO VAR1
VAR2.
DISPLAY 'STR1 = ' STR1.
DISPLAY 'VAR1 = ' VAR1.
DISPLAY 'VAR2 = ' VAR2.
GOBACK. |
which gives:
| Code: |
STR1 = A,B,C,D,E
STR1 = A\B,C,D,E
VAR1 = A
VAR2 = B,C,D,E |
This approach will work even if the delimited values are variable length, not just 1 byte. |
|
| Back to top |
|
 |
|
|
|