fhg
Volume 9, Number 5 -- February 4, 2009

A Bevy of BIFs: Look Up to %LookUp

Published: February 4, 2009

by 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


Sponsored By
WORKSRIGHT SOFTWARE

Do you need area code information?
Do you need ZIP Code information?
Do you need ZIP+4 information?
Do you need city name information?
Do you need county information?
Do you need a nearest dealer locator system?

We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

Just call us and we'll arrange for 30 days FREE use of either
ZIP/CITY or PER/ZIP4.

WorksRight Software, Inc.
Phone: 601-856-8337
Fax: 601-856-9432
E-mail: software@worksright.com
Web site: www.worksright.com


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Edwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

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


 
The Four Hundred
Hogging the Ground Day

IBM Sunsets i5/OS V5R4, Kills Older 595 Iron

MaxAva Gets Inventive With Subscription Model for HA

IBM Rejiggers Power Systems, System i and p Prices

Deconstructing and Rebuilding IBM's Q4 Server Sales

Four Hundred Stuff
i OS Vendors Take Different Approaches to Poor Economy

RPG Gets Mixed Up in EGL Jam

Vault400 Debuts Tiered DR and Managed HA Services

Zend's PHP to be Preloaded Onto IBM i OS

Bring Your IT Ideas to Life, mrc Says

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

System i PTF Guide
January 31, 2009: Volume 11, Number 5

January 24, 2009: Volume 11, Number 4

January 17, 2009: Volume 11, Number 3

January 10, 2009: Volume 11, Number 2

January 3, 2009: Volume 11, Number 1

December 27, 2008: Volume 10, Number 52

TPM at The Register
Sun taps ex-Merrill, ex-Fannie Mae exec for board

Intel to spill Nehalem secrets

Novell cuts 1000 100 workers

Ex-IntelCrayAkamai startup rejiggers virtualization

T3 girds loins for IBM legal fight

HP gives SMBs zero per cent financing

Citrix ejects 10 per cent of staff

US House OKs Obama's IT stimulus

Exploding core counts: Heading for the buffers

GeoCluster - Windows from here to Timbuktu

Sun will Rock in 2009

Big Sparc crunches Sun's Q2

Sun goes eco-friendly with data center compression

IBM whittles x64 iron prices

THIS ISSUE SPONSORED BY:

WorksRight Software
Help/Systems
System i Developer


Printer Friendly Version


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

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
Insert via Java

iSeries Access for Web

Mimix installation and configuration docs

EDI Inovis Programmer - Heavy Duty Problem Solver - Anytime

Data Queues vs. MQ Series: Performance

Removing blanks from a CL Variable

XML




 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2009 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement