Bryan's RPG IV Journal
%LOOKUP Function with ASCEND
Q: I recently ran into a problem with the %LOOKUP function (both in free and in fixed format). A converted program (which had originally used the LOOKUP operation) was using %LOOKUP on an array that was set up as ASCENDING. When the lookup occurred, no element was found. When I removed the ASCENDING keyword, the lookup word fine. Any ideas?
A: Unlike the LOOKUP operation, the %LOOKUPxx functions perform a binary search if the array is sequenced. This greatly improves performance, by reducing the number of comparisons that the lookup function must make. But a binary search relies on the fact that the elements are in sequence; if the entries are not in sequence, %LOOKUP may not get a hit.
Runtime arrays are not sequence checked by the compiler, so it's possible that the array was out of sequence despite ASCEND. The %LOOKUP function was depending upon ascending sequence, and gave up when it got to an element that was higher than the search argument. The problem can be solved by executing SORTA before the %LOOKUP, or by removing the ASCEND keyword.
ASCEND and DESCEND are only necessary when you are doing a high/low match compared to the search argument, and not for an equal match. For equal lookups ASCEND would not be necessary (or even desirable).
A: Unlike the LOOKUP operation, the %LOOKUPxx functions perform a binary search if the array is sequenced. This greatly improves performance, by reducing the number of comparisons that the lookup function must make. But a binary search relies on the fact that the elements are in sequence; if the entries are not in sequence, %LOOKUP may not get a hit.
Runtime arrays are not sequence checked by the compiler, so it's possible that the array was out of sequence despite ASCEND. The %LOOKUP function was depending upon ascending sequence, and gave up when it got to an element that was higher than the search argument. The problem can be solved by executing SORTA before the %LOOKUP, or by removing the ASCEND keyword.
ASCEND and DESCEND are only necessary when you are doing a high/low match compared to the search argument, and not for an equal match. For equal lookups ASCEND would not be necessary (or even desirable).
Log of Journal Entries |
|
Use this scrollable blog map to view other journal entries: |
|
| It is your responsibility to ensure procedures, techniques, and code used from this website are accurate and appropriate for your installation. No warranty or support is implied or expressed. |
![]() |
| Privacy and Email Policy |
| AS/400®, eServer, i5/OS, IBM i, Integrated Language Environment®, iSeries, OS/400®, RPG/400®, System i5, VisualAge®, and WebSphere® are trademarks of IBM Corporation. Note: IBM® System i is the latest member of the family of eServer iSeries. This site might refer to System i as System i5, iSeries, or AS/400. |
|
If you are having trouble navigating the menu at the top of this page, you may use the following scrollable site map instead: |
|
|











International visitors: