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

UNSTRING the First occurence Alone


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

New User


Joined: 25 Jul 2006
Posts: 24

PostPosted: Thu Apr 24, 2008 6:16 pm
Reply with quote

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
View user's profile Send private message
Bharath Bhat

Active User


Joined: 20 Mar 2008
Posts: 283
Location: chennai

PostPosted: Thu Apr 24, 2008 6:27 pm
Reply with quote

Hi,

Could you please post declarations of var1 and var2.
Back to top
View user's profile Send private message
rathinakarthik

New User


Joined: 25 Jul 2006
Posts: 24

PostPosted: Thu Apr 24, 2008 6:56 pm
Reply with quote

Hi,

var1 will be pic x(1)
var2 will be pix x(20)

Thanks
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Thu Apr 24, 2008 7:10 pm
Reply with quote

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
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Thu Apr 24, 2008 9:18 pm
Reply with quote

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
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Thu Apr 24, 2008 9:55 pm
Reply with quote

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
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 Handling the numeric data in unstring... COBOL Programming 18
No new posts Unstring COBOL Programming 4
No new posts UNSTRING problem COBOL Programming 3
No new posts UNSTRING a big string COBOL Programming 16
No new posts Unstring list of values into an array. COBOL Programming 8
Search our Forums:

Back to Top