RPGIV @ Work

A unique site for RPG and System i Lovers

Welcome!

Hi, this site will provide all what you need in System i and RPG developments.

My Name is Chamara Withanachchi, System i Expert and RPG Developer. And in the field for last 11 years.

I hope you will find lot of valuable information from this site

%SUBARR - Set/Get Portion of an Array Print E-mail
User Rating: / 0
PoorBest 
Written by Chamara Withanachchi   
%SUBARR(array:start-index{:number-of-elements})

Built-in function %SUBARR returns a section of the specified array starting at start-index. The number of elements returned is specified |by the optional number-of-elements parameter. If not specified, the number-of-elements defaults to the remainder of the array.


The first parameter of %SUBARR must be an array. That is, a standalone field, data structure, or subfield defined as an array. The first parameter must not be a table name or procedure call.


The start-index parameter must be a numeric value with zero |decimal positions. A float numeric value is not allowed. The |value must be greater than or equal to 1 and less than or equal to the number of elements of the array.


The optional number-of-elements parameter must be a numeric value with zero decimal positions. A float numeric value is not allowed. The value must be greater than or equal to 1 and less than or equal to the number of elements remaining in the array after applying the start-index value.


Generally, %SUBARR is valid in any expression where an unindexed array is allowed. However, %SUBARR cannot be used in the following places:


* as the array argument of built-in function %LOOKUPxx
* as a parameter passed by reference


%SUBARR may be used in the following ways:


* On the left-hand side of an assignment using EVAL or EVALR. This changes the specified elements in the specified array.
* Within the expression on the right-hand side of an assignment using EVAL or EVALR where the target of the assignment is an array. This uses the values of the specified elements of the array. The array elements are used directly; a temporary copy of the sub-array is not made.
* In Extended Factor 2 of the SORTA operation.
* In Extended Factor 2 of the RETURN operation.
* Passed by VALUE or by read-only reference (CONST keyword) when the |corresponding parameter is defined as an array.
* As the parameter of the %XFOOT builtin function.


Using %SUBARR


D a               s             10i 0 dim(5)
D b               s             10i 0 dim(15)
D resultArr       s             10i 0 dim(20)
D sum             s             20i 0 
 /free  
	    a(1)=9;  
     a(2)=5;  
     a(3)=16;  
     a(4)=13;  
     a(5)=3;  
     // Copy part of an array to another array:  
     resultArr = %subarr(a:4:n);     
          // this is equivalent to:     
          //   resultArr(1) = a(4)     
          //   resultArr(2) = a(5)     
          //   ...     
          //   resultArr(n) = a(4 + n - 1)  
 
     // Copy part of an array to part of another array:  
     %subarr(b:3:n) = %subarr(a:m:n);  
     // Specifying the array from the start element to the end of the array  
     // B has 15 elements and A has 5 elements.  Starting from element 2  
     // in array A means that only 4 elements will be copied to array B.  
     // The remaining elements in B will not be changed.  
     	b = %subarr(a : 2);  
 
     // Sort a subset of an array:  
     sorta %subarr(a:1:4);     
          // Now, A=(5 9 13 16 3);     
          // Since only 4 elements were sorted, the fifth element     
          // is out of order.  
          // Using %SUBARR in an implicit array indexing assignment  
     resultArr = b + %subarr(a:2:3)     
     // this is equivalent to:     
     //   resultArr(1) = b(1) + a(2)     
     //   resultArr(2) = b(2) + a(3)     
     //   resultArr(3) = b(3) + a(4)  
 
     // Using %SUBARR nested within an expression  
     resultArr = %trim(%subst(%subarr(stringArr:i):j));     
          // this is equivalent to:     
          //   resultArr(1) = %trim(%subst(stringArr(i+0):j))     
          //	 resultArr(2) = %trim(%subst(stringArr(i+1):j))     
          //   resultArr(3) = %trim(%subst(stringArr(i+2):j))  
 
     // Sum a subset of an array  
     sum = %xfoot (%subarr(a:2:3));     
          // Now sum = 9 + 13 + 16 = 38

Extracted from IBM

<Previous   Next>