Search code examples
ibm-midrangerpglerpg

How to subdivide the contents of a string variable based on a certain symbol in the content of the variable in RPGLE


Suppose I have a string variable in RPGLE . The content of the variable is "Hi;this;is;Kunal;Roy". How do I split the contents of the string based on the symbol ;

I want to have the values Hi , this, is , Kunal , Roy separated and stored in some other variable.

Can someone suggest a easy way of doing it in rpgle.


Solution

  • This is a really good question

    You could define an array to fill and then loop over the string

    D ArMax           c                    10                                       
    D ArVal           S                   dim(ArMax) like($MyString)                
    
    c                   movel     ';'           $Symbol           1                         
    c                   z-add     1             $Start            3 0                       
    c     *like         define    $Start        $Pos           
    c     *like         define    $Start        @@count        
           $MyString = 'Hi;this;is;Kunal;Roy';                                              
           $pos = %scan($Symbol: $MyString:$start);                                             
           dow $pos > 0;                                                                    
             @@count = @@count +1;                                                                                                                                                    
             ArVal(@@Count) = %subst($MyString:$Start:$pos-$Start);                         
             $start = $pos +1;                                                              
             $pos = %scan($Symbol: $MyString:$Start);                                           
           enddo;