Sunteți pe pagina 1din 6

An Array of Arrays | IBM i | IBM Systems Magazine

1 of 6

http://www.ibmsystemsmag.com/ibmi/developer/7216p2.aspx

Administrator
Backup and Recovery
DB2
High Availability
LPAR
Networks
Performance
Security
Systems Management
Tivoli
Trends
AIX
Linux
Open Source
What's New
Tips & Techniques
Application Development
Systems Management
Miscellaneous
Case Studies
Automotive
Healthcare
Manufacturing
Miscellaneous
Non-profit
Retail
Storage
Disk
Servers
Software
Tape
Product News
Buyer's Guide
Administrator
Backup and Recovery
DB2
Domino
High Availability
LPAR
Networks
Performance
Printing
Security
Systems Management
WebSphere
Windows Integration

Trends
IBM Announcements
Linux
Open Source
SOA
What's New
Tips & Techniques
Application Development

9/27/2010 10:51 AM

An Array of Arrays | IBM i | IBM Systems Magazine

2 of 6

http://www.ibmsystemsmag.com/ibmi/developer/7216p2.aspx

Systems Management
Case Studies
Automotive
Banking/Finance
Healthcare
Insurance
Manufacturing
Miscellaneous
Non-profit
Retail
Storage
Disk
Optical
Servers
Tape
Product News
Product Reviews
ENDPGM Main Page
Administrator
Backup and Recovery
CICS
DB2
High Availability
IMS
LPAR
Migration
Networks
Performance
Security
Systems Management
Tivoli
Trends
Linux
Open Source
Security
SOA
What's New
z/OS
z/VM
Tips & Techniques
Application Development
Systems Management
Case Studies
Automotive
Banking/Finance
Healthcare
Insurance
Manufacturing
Miscellaneous
Retail
Storage
Disk
Servers
Software
Tape
Product News
Stop Run
Buyer's Guide Main Page

9/27/2010 10:51 AM

An Array of Arrays | IBM i | IBM Systems Magazine

3 of 6

http://www.ibmsystemsmag.com/ibmi/developer/7216p2.aspx

Business Strategy
Competitive Advantage
Consolidation
Executive Perspective
Green IT
Migration
Open Source
ROI
Infrastructure
Blades
Storage
Systems Management
Case Studies
Distribution
Healthcare
Manufacturing
Services
Web 2.0
Cloud
Social Media
Trends
Collaboration
IBM Announcements
IBM 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

9/27/2010 10:51 AM

An Array of Arrays | IBM i | IBM Systems Magazine

4 of 6

http://www.ibmsystemsmag.com/ibmi/developer/7216p2.aspx

STORAGE
PRODUCT NEWS
ENDPGM
BUYER'S GUIDE

Administrator > Security

An Array of Arrays
July 2006 | by Jon Paris and 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, which
is 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 this
approach, 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:

9/27/2010 10:51 AM

An Array of Arrays | IBM i | IBM Systems Magazine

5 of 6

D validCustomer s

http://www.ibmsystemsmag.com/ibmi/developer/7216p2.aspx

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 1 2 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

9/27/2010 10:51 AM

An Array of Arrays | IBM i | IBM Systems Magazine

6 of 6

http://www.ibmsystemsmag.com/ibmi/developer/7216p2.aspx

Maximize your IT investment with monthly information from THE source...IBM Systems Magazine EXTRA & Marketplace eNewsletters.
SUBSCRIBE NOW.
View past IBM i EXTRAs here

IBM i
AIX
MAINFRAME
POWER
Homepage
About 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

9/27/2010 10:51 AM

S-ar putea să vă placă și