Home > AS/400 Tips > iSeries administrator tips > RPG data structure arrays improvement over multiple-occurrence data structures
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ISERIES ADMINISTRATOR TIPS

RPG data structure arrays improvement over multiple-occurrence data structures


Ron Turull
05.28.2003
Rating: -3.74- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



[IMAGE]
[IMAGE][IMAGE]
Ron Turull [IMAGE]
[IMAGE]

In case you haven't yet heard the news, the ILE RPG compiler now supports -- as of V5R2 -- data structure arrays. That is, you can now specify the Dim keyword on the definition of a data structure to dimension that data structure. Just as you can dimension a stand-alone field to create an array of that type of field, you can now dimension a data structure to create an array of that kind of data structure.

Data structure array basics
It's all pretty straightforward. If MyDS is a data structure array of 10 elements, then MyDS(5) will access the fifth element of that data structure array. But how do you access the subfields? That requires a bit more discussion.

When you define a data structure array, you not only use the Dim keyword, but you must also specify the Qualified keyword. The Qualified keyword was introduced in V5R1, and it allows you to specify the name of a data structure subfield qualified by the name of the data structure. For example, if data structure MyDS is defined with the Qualified keyword and it contains a subfield named Subfield1, then you would use the following notation to access the subfield:

If data structure MyDS is also defined with the Dim keyword, then something like the following notation is used to access subfields in a specific element:

In this example, you would be accessing the subfield named Subfield1 in the fifth element in the data structure array MyDS.

Clear up the confusion over multiple-occurrence data structures
Over the past few months I have detected a hint of confusion about multiple-occurrence data structures with regard to data structure arrays. While the two have some dissimilarities, data structure arrays are essentially and technically the same as multiple-occurrence data struc...


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
OS/400
Top 10 backup commands
Take control of your iSeries
How to save time using the CPYTOPCD and CPYFRMPCD commands
Top Q&A's on the OS/400
Top 10 security tips
Use caution when providing access to file shares
How to set up an autostart job
How does Sarbanes-Oxley affect you?
Automated disaster recovery revisited
Top 10 Administrator Tips

iSeries administrator tips
Translating Linux for IBM i admins: Using GUI to make it easy
Translating Linux for IBM i admins: Working with jobs and networking
OpenOffice: What to know before making the transition from Microsoft Office
OpenOffice: An enterprise open source solution
Database performance comparisons on IBM i
Translating Linux for IBM i admins: User profile commands
Modern System i reports using Client Access
Tips for installing Lotus Domino server on a System i partition
The iSeries Blog has a new home on IT Knowledge Exchange
Virtualization for IBM i: Backups

Systems Management
Can you trust all those trigger programs?
Are your backups complete?
Controlling remote command processing
Watch your profiles
Avoid locking issues
Send message to users at a remote site
Security journal receiver management
Top 10 backup commands
Tracking critical file access in real time
Create an iSeries Access image and update it with the latest Service Pack

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


tures.

More precisely, multiple-occurrence data structures are, and have always been, data structure arrays; they were just implemented poorly and given a goofy IBM name. Of course, this was the result of the six-character limit on fields names and on the Result Field column. Under the covers, data structure arrays and multiple-occurrence data structures are implemented almost identically. You see, a data structure is a high-level language (HLL) construct; there is no such thing as a data structure down at the machine interface (MI) level. That means all data structures must be implemented in a simulated fashion, regardless if they are simple data structures, multiple-occurrence data structures or data structure arrays. Interestingly, the MI language does contain arrays -- and fairly sophisticated ones at that. So, once a data structure is defined at the MI level, making it into an array is fairly straightforward.

Thankfully, we (i.e., the RPG language) have evolved to where we can actually call data structure arrays "arrays" instead of "multiple-occurrences." Unfortunately, most, if not all, of the documentation has not. I have yet to find one definitive statement that explains, for example, that multiple-occurrence data structures are often interchangeable with data structure arrays, such as when passed as parameters.

Arrays (this includes multiple-occurrence data structures because remember, they are arrays) are stored in one chunk of contiguous storage. There are no extra spaces in between the individual elements of the array unless you specify alignment; that is, the compiler will not add any extra space that you don't ask for. Therefore, in storage, a multiple-occurrence data structure looks exactly like a data structure array.

Another good example of the documentation being out of date is the SQL Reference. In the "ILE RPG," it discusses using multiple-occurrence data structures for multiple-record fetches. However, it makes no mention of using data structure arrays as an alternative, never mind that using data structure arrays is actually a better alternative.

Always use data structure arrays
If you are in charge of setting programming practices and standards in your shop, you should insist that data structure arrays are used instead of multiple-occurrence data structures whenever possible. They are much simpler to code, and they are much less prone to bugs.

-----------------------------------
About the author: Ron Turull is editor of Inside Version 5. He has more than 20 years experience programming for and managing AS/400-iSeries systems.

==================================
MORE INFORMATION
==================================

  • Don't proliferate old, bad code
    In an effort to hasten the completion of projects, most programmers "steal" code from other sources within their libraries. The problem is that many times, the code originated years ago and is cumbersome, lengthy and not easy to maintain. Even most ILE code resulted from old code that was passed through a conversion utility. Using the functionality of RPGIV's Keywords and Built-In-Functions usually can make the code much smaller and manageable. Search400.com memb er Joe Szymanski provides a couple of examples, including one involving data structure arrays.
  • Referencing a subfile of an array
    "JFuchs" is using a data structure as an array and want sto use one of the subfiles of the data structure in an Eval statement. The problem is he doesn't know how to reference this. Not to worry -- Nick Hobson was on hand with some advice.
  • "CLEAR" advantage over resetting work fields
    Site expert John Kohan illustrates how to use the "CLEAR" operation code to clear the fields in a a single-occurrence data structure or multiple-occurrence data structures.
  • Pass larger amounts of data between programs
    When using separate programs to retrieve and display data, Search400.com member Tim Cervenec says to use use arrays or multiple-occurrence data structures to pass back larger amounts of data strings.


Rate this Tip
To rate tips, you must be a member of Search400.com.
Register now to start rating these tips. Log in if you are already a member.




DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



iSeries Security - Security Tools, Physical Security and System Security
HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts