• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • A Bevy of BIFs: Look Up to %LookUp

    February 4, 2009 Susan Gantner

    The %LookUp built-in function is more than just a simple replacement for the LOOKUP operation code to allow use in /Free format logic. It offers the option of supercharging the performance of an array search operation.

    Let’s cover the basics first.

    %LOOKUP( searchfor : array {: startindex {: numelems}})
    

    The first parameter is the argument you’re searching for, and the second parameter the name of the array where you’re looking; i.e., what would have been specified in Factor 1 and 2 of the LOOKUP op code, respectively. The optional third parameter is the starting index for the search, i.e., what you could have specified as an index on the Factor 2 array.

    The fourth parameter is the number of elements to search. This option didn’t exist in the LOOKUP op code, so this represents the first enhancement offered by %LookUp. If your array is not full, you may now simply specify the number of elements that are there, avoiding searching the entire array when looking for an equal match.

    The return value of %LookUp is the element where a matching value was found, or zero if it was not found. There are variants of the function for comparisons less or greater than the search argument: %LookUpGT, %LookUpLE, etc.

    Perhaps the best feature of %LookUp when compared with the op code is the ability to use a binary search rather than the much slower linear search when the data in the array is in sequence (ascending or descending). If you’re not familiar with the notion of a binary search, imagine you’re searching for an exact match and there is no match in an array of 100 elements. A linear search (the type always used by the op code) would require 100 comparisons to determine there was no match. A binary search would begin around element 50 and compare. If the search argument is lower than the value of element 50, it then compares to element 25 for the second comparison. After each comparison, the number of candidate elements is cut in half. So in the worst case (a no match condition), it takes seven comparisons with a binary search compared to 100 in a sequential search. The performance difference grows exponentially as the size of the array gets larger. If you’d like to read more about linear vs. binary search methods, check out http://en.wikipedia.org/wiki/Binary_search.

    The LookUp operation code always performs a linear search, even if the array is sequenced. Before we had %Lookup, some programmers learned to speed up their searches by specifying both the EQ and GT indicators on LookUp (and initializing any “empty” elements with high values) so that the linear search stopped before the end of the array on a not found condition. This is certainly helpful, but using a binary search will still be much faster still.

    It is important to note that the binary search can only be used if the data in the array is in sequence and the programmer specifies that sequence by using either the Ascend or Descend keyword on the array definition. If it’s not practical to load the array in sequence, it could prove worthwhile to sort the array in the program if the search will be performed multiple times. This might be done using SORTA or the qsort C function.

    A mistake that is made more commonly than you might think is to specify Ascend or Descend on an array even when the values are not in sequence. This didn’t cause a problem for the LookUp operation code when searching for an equal match, but it would cause a problem for %LookUp’s binary search.

    So consider replacing your use of the operation code with %LookUp even if you’re not doing /Free form logic. If you’re already using %LookUp, you may also want to examine whether sequencing the array elements–and specifying that sequence on the definition–is worth the effort to make multiple subsequent searches orders of magnitude faster.

    Susan Gantner is one of the most respected System i gurus in the world and is one of the co-founders of System i Developer, an organization dedicated to RPG, DB2, and other relevant software technologies for the System i platform that hosts the new RPG & DB2 Summit conference. Gantner, who has worked in IBM’s Rochester and Toronto labs, left IBM to focus on training OS/400 and i5/OS shops on the latest programming technologies. She is also a regular speaker at COMMON and other user groups. Send your questions or comments for Susan to Ted Holt via the IT Jungle Contact page.



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    VISUAL LANSA 16 WEBINAR

    Trying to balance stability and agility in your IBM i environment?

    Join this webinar and explore Visual LANSA 16 – our enhanced professional low-code platform designed to help organizations running on IBM i evolve seamlessly for what’s next.

    🎙️VISUAL LANSA 16 WEBINAR

    Break Monolithic IBM i Applications and Unlock New Value

    Explore modernization without rewriting. Decouple monolithic applications and extend their value through integration with modern services, web frameworks, and cloud technologies.

    🗓️ July 10, 2025

    ⏰ 9 AM – 10 AM CDT (4 PM to 5 PM CEST)

    See the webinar schedule in your time zone

    Register to join the webinar now

    What to Expect

    • Get to know Visual LANSA 16, its core features, latest enhancements, and use cases
    • Understand how you can transition to a MACH-aligned architecture to enable faster innovation
    • Discover native REST APIs, WebView2 support, cloud-ready Azure licensing, and more to help transform and scale your IBM i applications

    Read more about V16 here.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    PowerTech:  Ensure your IBM i data is secure. Join a complimentary Webinar now!
    Vision Solutions:  Journaling for System i resilience. Learn more.
    COMMON:  Join us at the 2009 annual meeting and expo, April 26-30, Reno, Nevada

    IT Jungle Store Top Book Picks

    Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
    Getting Started with PHP for i5/OS: List Price, $59.95
    The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
    The System i Pocket RPG & RPG IV Guide: List Price, $69.95
    The iSeries Pocket Database Guide: List Price, $59.00
    The iSeries Pocket Developers' Guide: List Price, $59.00
    The iSeries Pocket SQL Guide: List Price, $59.00
    The iSeries Pocket Query Guide: List Price, $49.00
    The iSeries Pocket WebFacing Primer: List Price, $39.00
    Migrating to WebSphere Express for iSeries: List Price, $49.00
    iSeries Express Web Implementer's Guide: List Price, $59.00
    Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
    Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
    Getting Started with WebSphere Express for iSeries: List Price, $49.00
    WebFacing Application Design and Development Guide: List Price, $55.00
    Can the AS/400 Survive IBM?: List Price, $49.00
    The All-Everything Machine: List Price, $29.95
    Chip Wars: List Price, $29.95

    Remain Teams with Original to Combine Testing and Change Management Database Server/400, Anyone?

    Leave a Reply Cancel reply

Volume 9, Number 5 -- February 4, 2009
THIS ISSUE SPONSORED BY:

WorksRight Software
Help/Systems
System i Developer

Table of Contents

  • A Bevy of BIFs: Look Up to %LookUp
  • Treasury Of New DB2 6.1 Features, Part 1: Query Enhancements
  • Admin Alert: Time Gobbling Tasks for a System Upgrade

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25
  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle