An Array of Arrays _ IBM i ...2
Transcript of An Array of Arrays _ IBM i ...2
-
8/9/2019 An Array of Arrays _ IBM i ...2
1/6
-
8/9/2019 An Array of Arrays _ IBM i ...2
2/6
-
8/9/2019 An Array of Arrays _ IBM i ...2
3/6
Business Strategy
Competitive Advantage
Consolidation
Executive Perspective
Green IT
Migration
Open Source
ROI
Infrastructure
Blades
StorageSystems Management
Case Studies
Distribution
Healthcare
Manufacturing
Services
Web 2.0
Cloud
Social Media
Trends
Collaboration
IBM AnnouncementsIBM Research
Open Source
Social Media
What's New
Product News
AIX
MAINFRAME
POWER
Newsletters
About Us
Subscribe
Current Issue
Archive
IBM i
ALL EDITIONS
ADMINISTRATOR
DEVELOPER
TRENDS
TIPS & TECHNIQUES
CASE STUDIES
rray of Arrays | IBM i | IBM Systems Magazine http://www.ibmsystemsmag.com/ibmi/developer/721
9/27/2010 1
-
8/9/2019 An Array of Arrays _ IBM i ...2
4/6
STORAGE
PRODUCT NEWS
ENDPGM
BUYER'S GUIDE
Administrator> Security
An Array of ArraysJuly 2006 | by Jon Parisand Susan Gantner
Print Email
An Array of Arrays
The majority of the code is straightforward, so we won't discuss it in detail. At (A) you can see the definition of the array in which the customer
numbers will be stored. The entries are defined as being "LIKE" the customer number field. In addition, the array is loaded in key sequence, so we
also specified the ASCEND keyword, which ensures that the %LOOKUP BIF will use an efficient binary search. This approach will work as long as
we have fewer than 32,767 active customers.
At (B) we use the DSPLY op-code to ask the user to supply the number of test loops to run. Once the user has supplied this value, we start the timer
and invoke the sub-procedure that loads the customer numbers into the array. The procedure returns a count of the customer numbers loaded, whichis used later to limit the range of the search. Details of the array load subprocedure are at (D) above.
Within the test loop, we begin by calling GetRandom to obtain a random customer number. We then use the %LOOKUP BIF to search for that
customer in the array. Notice that we're using the count of the number of elements loaded into the array as the fourth parameter. This has the effect of
reducing the search time by limiting the search to the number of active elements in the array. Based on the result of the search, we then simply
increment the appropriate count and repeat the loop.
Once we've completed the required number of iterations, we exit the loop, stop the timer and display the results of the test (C). We'll look at the
results of the different methods later in this article.
As we noted earlier, our second program, CUSTSRCH2, uses an array where each element represents the status of a customer number. If the indicator
at a specific position is *On, then we consider the customer number to be valid. If it's *Off, then the customer number is invalid. As with the first
program, we're limited in this approach by RPG's array limit of 32,767 elements, but as long as the highest customer number in use is within that
range, this approach is simple to implement. So simple, in fact, that we won't take the space to describe it here - you can study the source if you wish
when you download the package. (Note: Although the program still reads the file in key sequence, this isn't strictly necessary when using thisapproach, and the load time could be shortened by reading the file in physical sequence.)
The third program, CUSTSRCH3, demonstrates an approach to the problem of RPG's array size limits. There are many techniques that we could use
as an alternative, including creating multiple arrays, each of which would relate to a specific range of account numbers. However, we've used a far
simpler (and it turns out much faster!) technique by simply employing a little bit of pointer arithmetic. If your pointer skills are rusty, or you are a little
"pointer-phobic", you might want to start out with the basic introduction to the use of based variables (read "Some Pointers on Using Pointers in RPG
IV"). Effectively what we'll do is the same as the compiler does "under the covers" to access an array element. We'll allocate sufficient memory to
accommodate all of our indicators, and then manipulate a pointer (pvalidCustomer) to position our indicator "view" over the appropriate one. This
almost certainly sounds more complicated than it really is, as you'll see when you study the code. We're effectively using the pointer as the array
index, but because we didn't have to define the storage as an array to RPG, we're no longer bound by the language's limits. In fact, we can easily
accommodate numbers up to 16 million with this technique (and with a little more work we can even extend that number). The first thing to change is
the definition of the "array" to:
rray of Arrays | IBM i | IBM Systems Magazine http://www.ibmsystemsmag.com/ibmi/developer/721
9/27/2010 1
-
8/9/2019 An Array of Arrays _ IBM i ...2
5/6
D validCustomer s n Based(pvalidCustomer)
Notice that it now appears to be a single named indicator. But the use of the BASED keyword will allow us to increment the pointer pvalidCustomer
to move its position through the "array." Naturally, the LoadCustomerNumbers routine also needs to be modified. The first significant change is to
reserve the required memory. In our example, we've used %ALLOC to reserve 100,000 bytes of dynamic memory. This is sufficient to accommodate
the full range of our five-digit customer number. We could've used a User Space to hold the "array," which has the advantage of allowing a single load
of the data to be shared between multiple jobs.
The next step is to initialize our "array" by setting all of the locations to *Off. This is necessary because we can't guarantee the content of dynamic
memory and some locations might already contain the value of *On. We've used the C function memset to do this - it's the fastest method we know of.
We won't detail its usage here - hopefully it's obvious from the code.
Next page: >>
Page 12 3
Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.
More Articles From Jon Paris
Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.
More Articles From Susan Gantner
Advertisement
WEBINAR -- RPG OA and Beyond from looksoftware Tuesday, September 28, 2010, 10 am BST (Europe)
Wednesday, September 29, 2010, 2 pm EST (USA)
Browse products and services for Administrator.
Advertisement
rray of Arrays | IBM i | IBM Systems Magazine http://www.ibmsystemsmag.com/ibmi/developer/721
9/27/2010 1
-
8/9/2019 An Array of Arrays _ IBM i ...2
6/6
Maximize your IT investment with monthly information from THE source...IBM Systems MagazineEXTRA & Marketplace eNewsletters.
SUBSCRIBE NOW.
View past IBM i EXTRAs here
IBM i
AIX
MAINFRAME
POWER
HomepageAbout Us
Contact Us
Subscriptions
Editorial Calendar
Advertise With Us
Reprints
Privacy Policy
Terms of Service
Sitemap
IBM Systems Magazine is a trademark of International Business Machines Corporation. The editorial content of IBM Systems Magazine is placed on
this website by MSP TechMedia under license from International Business Machines Corporation.
2010 MSP Communications, Inc. All rights reserved
rray of Arrays | IBM i | IBM Systems Magazine http://www.ibmsystemsmag.com/ibmi/developer/721