| RPG IV Array Lookup Using Built-in Functions |
|
|
| Written by Admin (Chamara) | ||
| May 05, 2009 at 01:05 PM | ||
|
Written by Jim Martin | ||
|
With V5R1, array lookup got a whole lot easier.
Array lookup has been around since RPG II, but the array built-in function was new with V5R1. Many RPG IV programmers don't use it yet--perhaps because they don't know how it works. %lookup(argument:Search-array::)
/free If %lookup(Input_Part:Part_Array) = *zero; Message = ‘Part entered, ‘ + Input_Part + ‘ is invalid.’; Endif; /end-free If you're searching for multiple occurrences of an argument in an array, use a starting index of one. Then, after a successful "hit," add one to the returned index to set the starting index before the next search. Otherwise, you will keep finding the same element. /free Res_Index = %lookup(Input_Part:Part_Array:100); If Res_Index > *zero; Part_Desc = Descr_Array(Res_Index); Part_ID = Input_Part; Else; Message = ‘Part number entered, ‘ + Input_Part + ‘ is invalid.’; Endif; /End-free Notice that the starting and resulting indexes are separate items now (different from the LOOKUP operation), so after a successful lookup, only the resulting index (the return value) is changed. If you choose a variable for the starting index, you can set it to any value desired, and it will not be altered by a %lookup built-in. D Search S 10 D Array S 10 Dim(32000) Based(Ptr) D Thous_Elem C 1000 D Res_Index S 5 0 D Increment S 2 0 Inz(1) /free // Allocate storage for 1000 elements Ptr = %alloc(Thous_Elem * %size(Array)); // Load 1000 elements here, then to do a look-up Res_Index = %lookup(Search:Array:1:Thous_Elem); // Later, when more storage is needed Increment += 1; // New increment operator as of V5R2 Ptr = %realloc(Ptr:Thous_Elem * Increment * %size(Array)); // After loading the next 1000 elements, the next (and following) // look-ups would be: Res_Index = %lookup(Search:Array:1:Thous_Elem * Increment); /end-free The other four array lookup built-ins are for argument relationships of the following:
|
||
| Last Updated ( Apr 20, 2010 at 11:06 PM ) | ||
| <Previous | Next> |
|---|