• 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
    LaserVault

    Integrate Virtual Tape to Automate Your Backups And Strengthen Your Ability To Recover From Cyber Attacks And Disasters

    With most IT departments stretched thin, finding something that can quickly free up IT time is definitely a bonus. That’s why it’s important to stop and take a look at integrating virtual tape into your backup and recovery. Virtual tape is one of those technologies where once you have it, you’ll wonder why you didn’t do it sooner. See a demo and get a $50 gift card.

    But what is it about using virtual tape that makes it so worthwhile? Why is it that so many IBM i shops are already using or considering using virtual tape for all or part of their backup and recovery systems?

    Virtual tape and virtual tape libraries offer a way to both simplify and strengthen backup and recovery operations. By incorporating virtual tape technology, automation of backups becomes possible resulting in hundreds of hours saved annually for IT departments and personnel.

    “We needed to find a replacement that would lower the maintenance cost and reduce complexity of our backup and recovery functions without a major disruption to our operations.” David Fray, Director of Enterprise Systems, ABC Financial

    LaserVault ViTL is a virtual tape and tape library solution developed specifically for use with IBM Power Systems (from AS/400 to iSeries to Power 9s). With ViTL you can:

    • Replace physical tape and tape libraries and eliminate associated delays
    • Automate backup operations, including the ability to purge or archive backups
    • Remotely manage your backups – no need to be onsite with your server
    • Save backups to a dedupe appliance and the cloud
    • Recover your data at lightspeed greatly improving your ability to recover from cyberattacks
    • And so much more

    Sign-up now to see a ViTL online demo and get a $50 Amazon e-gift card when the demo is complete as our way of saying thanks for your time. Plus when you sign-up you’ll receive a free facts comparison sheet on using virtual tape vs tape so you can compare the functionality for yourself.

    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

  • IBM Mulls Using DataMigrator as Cloud Warehouse Pipeline
  • PowerTech AV Automatically Detects Ransomware Activity
  • Infor Puts CM3 Project On Hold
  • Four Hundred Monitor, June 29
  • IBM i PTF Guide, Volume 24, Number 26
  • Guild Mortgage Takes The 20-Year Option For Modernization
  • IBM i Licensing, Part 3: Can The Hardware Bundle Be Cheaper Than A Smartphone?
  • Guru: The Finer Points of Exit Points
  • Big Blue Tweaks IBM i Pricing Ahead Of Subscription Model
  • We Still Want IBM i On The Impending Power E1050

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 © 2022 IT Jungle

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.