27
Fri, Dec
0 New Articles

SORTA with Key Fields

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

RPG IV has a little-known feature that allows you to sort an array by any given location. "How?" you may ask. The answer: data structures.

That's right; if you store an array inside of a data structure, RPG IV has an obscure syntax that allows you to specify a subfield that can be used to sort the array. The secret is to declare the array as a data structure subfield and use the OVERLAY keyword to build a mini data structure on top of the array.

The OVERLAY keyword allows you to force one data structure subfield to overlay another subfield. If the subfield being overlaid (specified as the first parameter of the OVERLAY keyword) is an array, the new subfield is also implicitly an array and overlays each element of the original array.

For example, assume an array named ARR appears in a data structure and has 50 elements, each being 30 characters in length. A second subfield named ITEM is declared as 10 positions in length and overlays the ARR subfield. In this situation, the ITEM subfield is implicitly declared as an array by the compiler with the same number of array elements as the ARR array (no DIM keyword is required, nor is one allowed). Each element of the ITEM array overlays the first 10 positions of each element of the ARR array.

The original array, ARR, does not require a length to be specified. A length may be specified, but if it is not, the compiler calculates the length by adding up the sizes of all the subfields that overlay ARR. Tricky? Yes! And certainly obscure too.

Let's take a look at an example of an array with overlapping subfields.

The requirement is to store 250 names and addresses for display in a subfile. A data structure is declared, and an array named THEARRAY is defined with 250 elements. No length is defined for THEARRAY as it will be calculated at compile time. Here is the declaration:

     D MyDS            DS                  Inz
     D TheArray                            Dim(250)

MYDS is the data structure name; although the name is irrelevant, I like to avoid unnamed data structures as a general practice. MYDS contains the subfield named THEARRAY. This subfield is an array with 250 elements.

The fields that will be displayed in a subfile are Customer Number (CUSTNO), Customer Name (CUSTNAME), City (CITY), State (STATE), and Zip Code (ZIPCODE). The end users require that they be able to sort the list by any of the given fields.

We add those subfields to the data structure and include the OVERLAY keyword to cause them to overlay the THEARRAY field, as follows:

     D MyDS            DS                  Inz
     D TheArray                            Dim(250)
     D  CustNo                        7P 0 Overlay(TheArray)
     D  CustName                     30A   Overlay(TheArray:*NEXT)
     D  City                         15A   Overlay(TheArray:*NEXT)
     D  State                         4A   Overlay(TheArray:*NEXT)
     D  ZipCode                      10A   Overlay(TheArray:*NEXT)

This code causes the compiler to declare that each of the overlaying subfields also be declared as arrays. CUSTNO, CUSTNAME, CITY, STATE, and ZIPCODE all implicitly have the DIM(250) keyword on them. But since they overlay THEARRAY, they are actually both data structure subfields and arrays, arrays that overlay THEARRAY in memory.

Let's look at a simpler example:

     D myDS            DS                  Inz
     D  Item                               Dim(5)
     D   Code                         1A   Overlay(Item
     D   Seq                          2A   Overlay(Item:*NEXT)  

In this example, the ITEM subfield is also an array with five elements. Looking further into the data structure, we see two more subfields named CODE and SEQ. The length of these two fields is 1 and 2 bytes, respectively. This means that ITEM is now an array of five elements, each with 3 bytes. By multiplying the number of elements by the number of bytes per elements, we get 15.

If we map out a memory diagram of this data structure and array, we would see the following (note that this is only a portion of the table for the sake of example):

Bytes
1
2
3
4
5
6
7
8
9
Data
A
0
1
B
1
2
C
3
4

ITEM(1)
ITEM(2)
ITEM(3)

Code(1)
Seq(1)
Code(2)
Seq(2)
Code(3)
Seq(3)

Note that each element of CODE and SEQ overlays the corresponding element of ITEM. That is, CODE (1) overlays position 1 of ITEM(1), and SEQ(1) overlays positions 2 and 3 of ITEM(1). Likewise, CODE (2) overlays positions 1 of ITEM(2), and SEQ(2) overlays positions 2 and 3 of ITEM(2). And so on.

We can sort the data in the ITEM array by ITEM, CODE, or SEQ. Whichever part we sort by, the other parts are carried along with it. Thus, if we sort by SEQ, the array ITEM is sorted using positions 2 and 3 of each of its elements.

If we wanted to create an array that contained contact information and needed to sort that information by any of its subfields, we would use the above technique. For example:

http://www.mcpressonline.com/articles/images/2002/SORTA%20with%20Key%20Fields11020500.png
(Click image to enlarge.)

In this example, the data structure named MYDS is a typical data structure. It contains the subfield named THEARRAY. This subfield is an array with 250 elements. Note the DIM(250) keyword. No length is specified for the THEARRAY subfield because it's implied, but the subsequent overlapping subfields--CUSTNO, CUSTNAME, CITY, STATE, ZIPCODE--are also part of the data structure, and they overlay the THEARRAY subfield.

This array may be sorted by any of the fields identified with an arrow or even by the THEARRAY field itself, however unlikely that would be.

Let's look at a working example. In the example the follows, the CUSTMAST file is read, and each record is stored in the CONTACT array. After all the records are loaded, the SORTA opcode is used to sort the contacts by the Balance Due. Here's the source code for this example:

     OPTION(*NODEBUGIO : *SRCSTMT)

     FCUSTMAST  IF   E             DISK    Prefix(CM_)

     D i               S             10i 0

     D Contact_T       DS                  Inz
     D Contact                             Dim(250)
     D  CstNbr                        7P 0 Overlay(Contact)
     D  CompName                     30A   Overlay(Contact:*NEXT)
     D  FName                        15A   Overlay(Contact:*NEXT)
     D  LName                        15A   Overlay(Contact:*NEXT)
     D  Address                      30A   Overlay(Contact:*NEXT)
     D  City                         25A   Overlay(Contact:*NEXT)
     D  State                         5A   Overlay(Contact:*NEXT)
     D  ZipCode                      10A   Overlay(Contact:*NEXT)
     D  Country                      20A   Overlay(Contact:*NEXT)
     D  Balance                       7P 2 Overlay(Contact:*NEXT)

     C                   READ      CustRec
     C                   DOW       NOT %EOF(CUSTMAST)
     C                   eval      i = i + 1
     C                   eval      CstNbr(i)   = CM_CustNO
     C                   eval      CompName(i) = CM_CompName
     C                   eval      FName(i)    = CM_FstName
     C                   eval      LName(i)    = CM_LstName
     C                   eval      Address(i)  = CM_Addr1
     C                   eval      City(i)     = CM_City
     C                   eval      State(i)    = CM_State
     C                   eval      ZipCode(i)  = CM_ZIPCODE
     C                   eval      Country(i)  = CM_Country
     C                   eval      Balance(i)  = CM_BALDUE
     C                   read      CustRec
     C                   Enddo

      *** Sort the contact list by Balance Due
     C                   SortA     Balance

     C                   eval      *INLR = *ON
     C                   return

Of course, all the normal array-sorting issues apply to this example; you have to be aware of unused array elements and also watch for too many records overrunning the array. Using dynamic arrays could help with this, particularly if you extract the number of records you're going to read from the File's INFDS and then allocate enough elements for the array dynamically. See my two previous articles on this topic: "Best of Cozzi: Dynamic Memory and Dynamic Arrays" and "Dynamic Arrays Revisited."

If you want to avoid sorting empty array elements, you will need to keep track of how many elements you've populated and use the new OS/400 V5R3 enhancement %SUBARR. For example:

     C                   sorta     %subarr(Balance:1:nNumElems)

See my article on "Sorting and Searching Arrays Becomes Manageable in V5R3" for more information.

Bob Cozzi is a programmer/consultant, writer/author, and software developer of the RPG xTools, a popular add-on subprocedure library for RPG IV. His book The Modern RPG Language has been the most widely used RPG programming book for nearly two decades. He, along with others, speaks at and runs the highly-popular RPG World conference for RPG programmers.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: