RPGLE Split

RPGLE

The javascript split method splits a string into an array of substrings. Consider the following code:

var string1 = "blue+++green+++yellow+++fuchsia"; var colours = string1.split("+++");

The result is that colours is an array with elements that contain "blue", "green", "yellow" and "fuchsia".

Wouldn't it be great if there was a split built in function in RPGLE? Sorry, there isn't. You will have to write your own.

Or copy mine:

              
							 P split           b                                                                            
							 D split           pi          1000    dim(50)                                                  
							 D  data                       8000    const                                                    
							 D  delimiter                    10    const                                                    
																															
							 D returnArray     s           1000    dim(50)                                                  
																															
							 D startPos        s             10i 0                                                          
							 D foundPos        s             10i 0                                                          
							 D increment       s             10i 0                                                          
							 D index           s              5i 0 inz(1)                                                   
							  /free                                                                                         
							   if data <> *blanks;                                                               
								 increment = %len(%trim(delimiter));                                                        
								 startPos = 1;                                                                              
								 dou foundPos = %len(data) + 1;                                                  
								   foundPos = %scan(%trim(delimiter):data:startPos);                                        
								   if foundPos = 0;                                                              
									 foundPos = %len(data) + 1;                                                             
								   endif;                                                                        
								   returnArray(index) = %subst(data:startPos:foundPos - startPos);                          
								   index += 1;                                                                              
								   startPos = foundPos + increment;                                                         
								 enddo;                                                                          
							   endif;                                                                            
																															
							   return returnArray;                                                                          
							  /end-free                                                                                     
							 P split           e                                                                         
						

This split function expects an input string 8000 characters long, a delimiter (10 characters long) and passes back an array containing the delimitered substrings. You can change the length of the delimiter, if 10 characters isn't enough, and you don't have to change any of the code for it to continue to work. The %len(%trim(delimiter)) takes care of that.

In use, in free format RPG, it looks like this:

colours = split(string1:'+++');
						

Where colours is an array with 50 elements, each 1000 characters long.