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
.
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
LATEST COMMENTS
MC Press Online