Fetch values in an array

Use @Fetch functions to process values that have been separated by commas or any other character.

Often we needed to process values that we obtain from other applications that been separated by commas or any other

character. This process can be laborious.

With the @Fetch functions, the process is very easy. We put the excellent values in an array that later we will process.

@Fetch Fetch in a array the comma separated values of a string.

Format: Eval Array = @Fetch('one, two, three, four') or Eval Array = @Fetch('one*two*three*four':*Off:'*')

If you want a %%%EOF%%% mark, set the second parm to *On

If you want to sepparate the values with a char different like a comma, put it in the threeth parm.

 To run it, copy the prototype with the /COPY stament and define a array: 

d ac_Deduc        s            250a   Dim(100)  

We have a variable called ftTXT2 with the value 1,3,,5,7

c                   Eval      ac_Deduc = @Fetch(ftTXT2:*On)    

After this programm steep, the array ac_Deduc have this values
(1) -> '1'
(2) -> '3'
(3) -> ''
(4) -> '5'
(5) -> '7'
(6) -> '%%%EOF%%%'
(7..100) -> '' 

We put the second parm at *ON because we will know the end of the values, to differentiate the blank value of the position 3 in the array of the rest of blank results after the end of data.

Don't forget to link the @.FETCH module when you make the *PGM with the CRTPGM command.


The prototype @P.FETCH :

 
d*                                                                   
d* Coloquem una cadena separa per comes en una array                 
d  @Fetch         PR           250a   Dim(100)                       
d    p_Valor                 24850a   Value                          
d    p_EOF                        n   Value Options(*NOPASS)         
d    p_Separador                 1a   Value Options(*NOPASS)         




The module @.FETCH :

h*                                                                         
h*                                                                         
h*                                                                         
h* @Fetch    Fetch in a array the comma separated values of                
h*           a string.                                                     
h* Format:   Eval Array = @Fetch('one,two,three,four')                     
h*       or  Eval Array = @Fetch('one*two*three*four':*Off:'*')            
h*           If you want a %%%EOF%%% mark, set the second parm             
h*           to *On                                                        
h*           If you want to sepparate the values with a char               
h*           different like a comma, put it in the threeth parm.           
h*                                                                         
h*                                                                         
h* @Fetch    Coloquem el valor d'una cadena separada per comes             
h*           en una array.                                                 
h* Format:   Eval Array = @Fetch('u,dos,tres,quatre')                      
h*        o  Eval Array = @Fetch('u*dos*tres*quatre':*Off:'*')             
h*           Si vols una marca %%%EOF%%%, posa a *On el segon              
h*           parametre.                                                    
h*           Si vols separar els valors amb un altre caracter              
h*           diferent de la coma, coloca'l com a tercer parametre.         
h NoMain                                                                   
h Option(*NODEBUGIO : *SRCSTMT)                                            
h DATFMT(*EUR)                                                  
h*                                                              
d*                                                              
d* Definicio del procediment (prototip)                         
d/COPY utlpgmsrc,@p.fetch                                       
d*                                                              
p*---------------------------------------------------           
p* Col*loquem una cadena a un Array indicant un                 
p* separador d'elements.                                        
p*---------------------------------------------------           
p @Fetch          B                   Export                    
d @Fetch          PI           250a   Dim(100)                  
d    p_Valor                 24850a   Value                     
d    p_EOF                        n   Value Options(*NOPASS)    
d    p_Separador                 1a   Value Options(*NOPASS)    
d*                                                              
d  j_Return       s            250a   Dim(100)                  
d  j_p            s              3p 0                           
d  j_pV           s              5p 0                           
d  j_pV2          s              5p 0                           
d  j_EOF          s               n   Inz(*Off)                 
d  j_Separador    s              1a   Inz(',')                  
 /Free                                                          
                                                                
  // Natejem el resultat final i determinem quin es el separador                                     
  Clear j_Return;                                                                                    
  If  %Parms >= 2;                                                                                   
      j_EOF = p_EOF;                                                                                 
  EndIf;                                                                                             
  If  %Parms = 3;                                                                                    
      j_Separador = p_Separador;                                                                     
  EndIf;                                                                                             
                                                                                                     
   // Iniciem el bucle per recuperar els 99 possibles valors                                         
  j_pV2 = 1;                                                                                         
  For j_p = 1 to 99;                                                                                 
                                                                                                     
   // Si cerquem mes enlla de la llargaria de la cadena, sortim                                      
      If j_pV2 > %len(%trim(p_Valor));                                                               
         Leave;                                                                                      
      EndIf;                                                                                         
                                                                                                     
      j_pV = %Scan(j_Separador:%trim(p_Valor):j_pV2); // Recuperem la possicio del seguent separador 
                                                                                                     
      If j_pV = 0;    // Si no trobem res, processem el darrer i sortim del bucle                    
         j_pv = %len(%trim(p_Valor))+1;                                                              
         j_Return(j_p) = %Trim(%Subst(p_valor:(j_pV2):(j_pV-j_pV2)));                                
         Leave;                                                                                      
      EndIf;                                                                
                                                                            
      If j_pV = j_pV2;    // Si no hi habien espais entre dues comes        
         j_pV2 = j_pV + 1;                                                  
         Iter;                                                              
      EndIf;                                                                
                                                                            
      // Li passem el valor a l'array i resituem el punter                  
      // de recerca.                                                        
      j_Return(j_p) = %Trim(%Subst(p_valor:(j_pV2):(j_pV-j_pV2)));          
      j_pV2 = j_pV + 1;                                                     
                                                                            
  EndFor;                                                                   
                                                                            
   // Si han indicat que volen marca EOF, li coloquem                       
   If j_EOF;                                                                
     j_Return(j_P+1) = '%%%EOF%%%';                                         
   EndIf;                                                                   
                                                                            
   // Retornem l'array recomposada                                          
  Return j_Return;                                                          
 /End-Free                                                                  
c*                                                                          
p @Fetch          E                                                         

==================================
MORE INFORMATION ON THIS TOPIC
==================================

The Best Web Links: tips, tutorials and more.

Ask your programming questions--or help out your peers by answering them--in our live discussion forums.

Ask the Experts yourself: Our application development gurus are waiting to answer your programming questions.

This was first published in December 2002

Dig deeper on iSeries CL programming

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchEnterpriseLinux

SearchDataCenter

Close