| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| NASCAR9 Intermediate
 
 
 Joined: 08 Oct 2004
 Posts: 274
 Topics: 52
 Location: California
 
 | 
			
				|  Posted: Thu May 21, 2009 11:33 am    Post subject: Split a String into 2 seperate strings |   |  
				| 
 |  
				| I have searched and tested without any luck   
 I need to split a string into two strings. The string is fixed but the data in the string is not. I need to break the string at no more than the first 14 characters
 ( ' ' or SPACE  is where the break should occur) , with the rest on a separate line.
 
 Please see examples below.
 
  	  | Code: |  	  | INPUT
 The house has a red roof.
 The house in the field has a green roof.
 That house has white trim with a red door.
 
 | 
 
 
  	  | Code: |  	  | Output should look like this:
 The house has
 a red roof.
 
 The house in
 the field has a green roof.
 
 That house
 has white trim with a red door.
 
 
 | 
 _________________
 Thanks,
 NASCAR9
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| kolusu Site Admin
 
  
 
 Joined: 26 Nov 2002
 Posts: 12394
 Topics: 75
 Location: San Jose
 
 | 
			
				|  Posted: Thu May 21, 2009 12:45 pm    Post subject: |   |  
				| 
 |  
				| Nasacar9, 
 Unless I am gravely mistaken isnt it simple task of checking the 14th byte for a space and if it is a space just move it or scan for a space from pos 14 back wards in the input? you can add error checking to it if you don't find a space in the first 14 bytes.
 
 untested code
 
 
  	  | Code: |  	  | 01 INP-STR          PIC X(nn).
 01 OUT-STR1         PIC X(14).
 01 OUT-STR2         PIC X(nn-14).
 01 WS-SUB           PIC S9(04) COMP.
 01 S-SPACE-FOUND    PIC X VALUE 'N'.
 
 IF INP-STR(14: 1) = ' '
 MOVE +14    TO WS-SUB
 PERFORM 1000-MOVE-TO-OUTPUT
 ELSE
 MOVE 'N' TO S-SPACE-FOUND
 PERFORM VARYING WS-SUB FROM 14 BY -1
 UNTIL WS-SUB = 1 OR S-SPACE-FOUND = 'Y'
 IF INP-STR(WS-SUB : 1) = ' '
 PERFORM 1000-MOVE-TO-OUTPUT
 MOVE 'Y' TO S-SPACE-FOUND
 END-IF
 END-PERFORM
 END-IF
 
 1000-MOVE-TO-OUTPUT.
 
 MOVE INP-STR(0001: WS-SUB)   TO OUT-STR1
 MOVE INP-STR(WS-SUB + 1 :)   TO OUT-STR2
 
 DISPLAY 'OUT-STR1 : ' OUT-STR1
 DISPLAY 'OUT-STR2 : ' OUT-STR2
 
 | 
 
 
 PS: I granted you edit authority to edit your OWN posts
 
 Kolusu
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| NASCAR9 Intermediate
 
 
 Joined: 08 Oct 2004
 Posts: 274
 Topics: 52
 Location: California
 
 | 
			
				|  Posted: Thu May 21, 2009 2:01 pm    Post subject: |   |  
				| 
 |  
				| Thanks Kolusu! I have been testing with STRING and UNSTRING. I was hoping I could get a REVERSE STRING/UNSTRING to get my results. The string I need to break is 200 characters, I used 14 as an example. The logic works the same regardless. I have an UNSTRING FUNCTION REVERSE I
 _________________
 Thanks,
 NASCAR9
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| kolusu Site Admin
 
  
 
 Joined: 26 Nov 2002
 Posts: 12394
 Topics: 75
 Location: San Jose
 
 | 
			
				|  Posted: Thu May 21, 2009 2:59 pm    Post subject: |   |  
				| 
 |  
				| NASCAR9, 
 on second thoughts I think it can be simplified. Here is another untested code.
 
 
  	  | Code: |  	  | IF INP-STR(19:1) = ' '
 MOVE +19    TO WS-SUB
 ELSE
 INSPECT FUNCTION REVERSE(INP-STR(1:19))
 TALLYING WS-TALLY FOR CHARACTERS BEFORE ' '
 COMPUTE WS-SUB = 19 - WS-TALLY
 END-IF
 
 MOVE INP-STR(0001: WS-SUB)   TO OUT-STR1
 MOVE INP-STR(WS-SUB + 1 :)   TO OUT-STR2
 
 | 
 
 btw what happens if you don't find a space in the first 19 bytes? will the string1 be empty?
 
 Kolusu
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| NASCAR9 Intermediate
 
 
 Joined: 08 Oct 2004
 Posts: 274
 Topics: 52
 Location: California
 
 | 
			
				|  Posted: Thu May 21, 2009 3:15 pm    Post subject: |   |  
				| 
 |  
				| Thanks Kolusu, 
 I will test your new logic
 
 The string is 200 characters, it needs to be split around 125, the data is medical procedure descriptions, so I'm positive I will find a blank.
 
 Thanks again!
 _________________
 Thanks,
 NASCAR9
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| NASCAR9 Intermediate
 
 
 Joined: 08 Oct 2004
 Posts: 274
 Topics: 52
 Location: California
 
 | 
			
				|  Posted: Thu May 21, 2009 4:02 pm    Post subject: |   |  
				| 
 |  
				| Kolusu, I like your last post. It gives the correct results and it's easy to understand.
 Here are the results:
 
  	  | Code: |  	  | WK-STRING               = This is a test;This is  part2 of the test
 
 
 
 IF WK-STRING(19:01)     = ' '
 MOVE +19    TO WS-SUB
 ELSE
 INSPECT FUNCTION REVERSE(WK-STRING(1:19))
 TALLYING WS-TALLY FOR CHARACTERS BEFORE ' '
 COMPUTE WS-SUB = 19 - WS-TALLY
 END-IF
 
 MOVE WK-STRING(0001: WS-SUB)   TO OUT-STR1.
 MOVE WK-STRING(WS-SUB + 1 :)   TO OUT-STR2.
 
 WS-SUB                  = 010
 WS-TALLY                = 009
 OUT-STR1                = This is a
 OUT-STR2                = test;This is  part2 of the test
 
 | 
 
 
  	  | Code: |  	  | 
 IF WK-STRING(20:01)     = ' '
 MOVE +20    TO WS-SUB
 ELSE
 INSPECT FUNCTION REVERSE(WK-STRING(1:20))
 TALLYING WS-TALLY FOR CHARACTERS BEFORE ' '
 COMPUTE WS-SUB = 20 - WS-TALLY
 END-IF
 
 MOVE WK-STRING(0001: WS-SUB)   TO OUT-STR1.
 MOVE WK-STRING(WS-SUB + 1 :)   TO OUT-STR2.
 
 WS-SUB                  = 020
 WS-TALLY                = 000
 OUT-STR1                = This is a test;This
 OUT-STR2                = is  part2 of the test
 
 | 
 _________________
 Thanks,
 NASCAR9
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		|  | 
	
		|  |