29
Fri, Nov
0 New Articles

Formatting Your Data for Output

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

As the RPG language continues to evolve, more and more tools have been put at our disposal. New data types and BIFs have been added to make our jobs easier. Make sure that you know how to use the tools at your disposal.

Unless you are living the life of an ostrich (your head buried in the sand), surely you will have noticed that the AS/400 has continued its technological evolution at a mind- boggling pace. Changes to the architecture and operating system have occurred at a rate never before seen in the midrange-computing market. Programming languages are changing, too. The changes being announced on a daily basis are almost impossible to keep up with.

Many new additions have been made to the RPG language. Included among them are built-in functions (BIFs), better string-handling tools, and the ability to use a variety of new data types. We would like to take this opportunity to get on our soapbox and show you how these new additions may be used to make some everyday programming jobs considerably easier.

Getting Packed

In Figure 1, we used the evaluate (EVAL) operation code in conjunction with the %TRIM built-in function to pack a person’s first name, middle initial, and last name into a single output field.

The three database fields that will be concatenated with this operation are FirstName, Initial, and LastName. The first EVAL operation packs the FirstName and Initial, leaving a blank in between the fields. The %TRIM built-in function is used to trim both the leading and trailing blanks from the FirstName field. A second EVAL operation is used to pack the LastName field to the results of the first EVAL operation (leaving one space in between).

The entire operation is performed in two freeform statements to allow for the possibility that the Initial field may contain a blank. (Failure to trim the blanks from the result of the first EVAL operation could result in a condition in which there are two blanks between the FirstName and LastName fields when the initial field is blank.) The result of

this operation would turn three separate fields, John, Q, and Public into a single output field as John Q Public.

Make It a Date!

The date and time data types have been around for some time. But the ability to use these data types in your RPG programs is one of the new capabilities that was announced with RPG IV. Even if you choose not to use these special data types in your database, they can be useful tools in your RPG programs. In Figure 2, we show how the date data type can be used to perform quick-and-easy date conversions from one format to another.

In the example, you can see that we have defined four fields: a six-digit input date (InputDate), a date work field (WorkDate), an eight-character alphanumeric output field (PrtAlfDate), and a six-digit numeric output field (PrtNumDate). The date work field is defined with a date data type (as designated by the D data type), and is used to perform our date conversion from one date format to another. In our example, we convert our six-digit input date field (InputDate) stored as yymmdd to its numeric output format of mmddyy (PrtNumDate) as well as to its alphanumeric mm/dd/yy equivalent (PrtAlfDate).

In the case of the alphanumeric representation, the conversion inserted the separator characters into the output fields for us. The separator character used is dependent upon the date data type specified in factor 1 when the date data type work field is moved to the output field.

Does Anybody Really Know What Time It Is?

Another way we like to exploit the advantage of the new data types is by using the time data type to convert the system time from military time to the more accepted *USA (Hours:Minutes am/pm) format.

The time data type can be used in a fashion that is similar to the preceding example, where we used the date data type. In the example outlined in Figure 3, we used the TIME op code to retrieve the system time (storing it in the field named Time) and then moved it to the work field named WorkTime. You will note that the work field is defined with a T data type, which designates that the field represents a time data type. The first MOVE operation converts the system time from the military-time format, which was retrieved from the system value via the TIME op code, to our WorkTime field, which was defined with the time data type.

Once that operation was performed, we could use the second MOVE operation to put the time in any format we wish. In our example, we chose to use the *USA format as described above. Using the code in Figure 3, the system time of 12:50:00 would be converted to 12:50 p.m.

Your Account Balance Is...

Seeing as how the two of us have been around since the time of the dinosaurs, we could easily pass along some horror stories about formatting data. You would not believe all of the hoops we had to jump through in the past to format data in such a way that it could be read. But, since most of you youngsters out there were not working in data processing prior to the invention of the personal computer, our stories probably wouldn’t mean much to you.

However, trust us when we tell you that one of the most useful additions to the RPG language in the last few years is the advent of the BIF. We saw the power of the %TRIM BIF in Figure 1 when we used it to trim leading and trailing blanks from our character-output fields. In Figure 4, we used the Edit Code (%EDITC) BIF to apply an Edit Code against a numeric field. As you can see from the code, we applied a J Edit Code against the numeric Amount field and added a floating dollar sign ($). Using the code in Figure 4, an account balance (Amount) of -$249.95 would be converted into the output field named Balance as “Your account balance is $249.95-.”

The Phone Is for You

Along the same lines as the preceding example, the Edit Word (%EDITW) BIF can be used to apply an Edit Word against a numeric field. In Figure 5, we applied the

%EDITW BIF against a 10-digit numeric phone number, the Phone field, to create our desired output.

We first established the Edit Word we wanted to use as a named constant (as designated by a C data type) and named the Edit Word PhoneEdit. We then defined our output field named PrintPhone. We applied the Edit Word to the Phone input field using the %EDIT BIF and the EVAL operation. Using the code in our example, the phone number of 1235551234 would be converted into the PrintPhone output as (123) 555-1234.

A Very Important Date

In our sixth and final example, we chose to combine the power of data types and BIFs to show you how a numeric six-digit date can be easily converted to an alphanumeric representation, including the name of the month and day of the week that the date represents.

Take a look at the code in Figure 6. We began by defining two compile-time arrays to represent the names of the month (Months) and days of the week (Days), respectively. We then defined the work and output fields that we will use in our calculations. Note that two of the fields (BaseDate and WorkDate) are defined with the date data type, and that we established an initial value in the BaseDate field. As you will see, it is the initial value established in this field that will allow us to convert a date to the day of the week.

The calculation specifications begin by converting the six-digit numeric date to a date data type, using the MOVE operation code. It is important to note that the input value must always be a valid date and must always be stored as yymmdd, as specified in factor 1 of the MOVE operation. If this is not the case, the code will result in a runtime error. You could easily test the input field for validity prior to conversion using the TEST op code.

We then subtracted the BaseDate value of January 1, 1901, from the date being converted using the Subtraction Duration (SUBDUR) operation. We placed the results in a field named WorkField. The results of the SUBDUR operation were then divided by seven (the number of days in a week) with the remainder being placed in the field named DayIndex. We then incremented the DayIndex field to avoid getting an array index error.

We used the Extract (EXTRCT) op code to parse out the month, day, and year from the original input date. The EVAL statement is then used to put all of the work fields together and create a formatted date field that includes the day-of-week and month name. When the code in Figure 6 is applied to the input date of 12/01/98, the PrintDate output field would be formatted as “Tuesday, December 01, 1998.”

RPG IV to the Rescue!

Some of our readers drop us a line now and then, responding to the various articles we have written. We were recently challenged by one of them to show why he should convert his shop to ILE and RPG IV. Our reader had been using RPG III for years and was quite happy with the results. He saw no real advantage to converting his shop to RPG IV because RPG III was working just fine for him.

We have been working in RPG IV for a couple of years now. We see many advantages to RPG IV, but they are not always easy to put into words. They are sometimes easier to put into code. We hope that the examples in this article will help to convince our challenger, and other readers, to see that RPG IV is the best way to go.

Doug Pence is the founder and Ron Hawkins is the research and development manager of Computer Processing Unlimited, Inc. in San Diego, California. They are also the authors of Power RPG III and Power RPG IV, published by Midrange Computing. Doug Pence can be reached by email at This email address is being protected from spambots. You need JavaScript enabled to view it.. Ron Hawkins can be reached by email at This email address is being protected from spambots. You need JavaScript enabled to view it.
.

CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++++++++++++++++++++++++
c Eval PrintName = %trim(FirstName) +
c ' ' + Initial
c Eval PrintName = %trim(PrintName) +
c ' ' + LastName

Figure 1: Packing first name, middle initial, and last name into a single output field

DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++
d InputDate s 6 0
d WorkDate s d
d PrtAlfDate s 8
d PrtNumDate s 6 0

CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++++++++++++++++++++++++
c *ymd move InputDate WorkDate
c *mdy move WorkDate PrtAlfDate
c *mdy move WorkDate PrtNumDate

Figure 2: Formatting numeric input date (stored as yymmdd) for output

DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++
d Time s 6 0
d WorkTime s t
d PrintTime s 8
CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++++++++++++++++++++++++

* prepare current time for output
c time time
c *hms move time Worktime
c *usa move WorkTime PrintTime

Figure 3: Formatting time for alphanumeric output

DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++
d Balance s 60

CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++++++++++++++++++++++++

* Reformat amount
c eval Balance = 'Your account balance is ' +
c %trim(%editc(amount:'J':'$'))

Figure 4: Using the %EDITC BIF to format output fields

DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++
d PhoneEdit c '0( )& - '
d PrintPhone s 15

CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++++++++++++++++++++++++

* Reformat phone number
c eval PrintPhone = %editw(phone:phoneedit)

Figure 5: Using the %EDITW BIF to format output fields

DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++
d ds
d Months 11 dim(12)
d January 1 11 inz('January')
d February 12 22 inz('February')
d March 23 33 inz('March')
d April 34 44 inz('April')
d May 45 55 inz('May')
d June 56 66 inz('June')
d July 67 77 inz('July')
d August 78 88 inz('August')
d September 89 99 inz('September')
d October 100 110 inz('October')
d November 111 121 inz('November')
d December 122 132 inz('December')

d ds
d Days 9 dim(7)
d Tuesday 1 9 inz('Tuesday')
d Wednesday 10 18 inz('Wednesday')
d Thursday 19 27 inz('Thursday')
d Friday 28 36 inz('Friday')
d Saturday 37 45 inz('Saturday')
d Sunday 46 54 inz('Sunday')
d Monday 55 63 inz('Monday')

d BaseDate s d inz(d'1901-01-01')
d Day s 2 0
d DayIndex s 2 0
d MnthIndex s 2 0
d Year s 4 0
d WorkDate s d
d WorkField s 7 0
d PrintDay s 2
d PrintYear s 4
d PrintDate s 40

CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++++++++++++++++++++++++

* Reformat date and print it as "day of week", month, day...
c *ymd move InputDate WorkDate
c WorkDate SubDur Basedate Workfield:*D
c Div 7 Workfield
c mvr DayIndex
c Eval DayIndex = (DayIndex + 1) *

c Extrct WorkDate:*m MnthIndex
c Extrct WorkDate:*d Day
c move Day PrintDay
c Extrct WorkDate:*y Year
c move Year PrintYear

* format data to be printed...
c Eval PrintDate = %trim(Days(DayIndex)) +
c ', ' + %trim(Months(MnthIndex)) + ' ' +
c %trim(PrintDay) + ', ' + PrintYear

Figure 6: Formatting date for alphanumeric output

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: