One thing we need to do as programmers is keep current on our RPG IV skills. By "keeping current," I mean knowing what features of the language are available and what they can do for us.
RPG IV is an interesting language. In other languages, such as C and Java, the languages themselves do very little; you have If, Then, Else, CASE, Do, and the ability to call subprocedures or functions. To add functionality to the language, you need to buy an add-on library or hope the language manufacturer provides one with your compiler.
My own RPG xTools add-on library consists of about 200 prewritten and ruggedly tested RPG IV subprocedures. They act just like RPG IV's built-in functions. But RPG IV has a lot of its own functions that, in other languages, would normally only be available in add-on libraries.
When asked about the best way to do a simple task, I often get the Rube Goldberg solution along with the question. For example: "How do I...? Here's what I did." They ask the question and then give me a clumsy answer. Not a bad answer, mind you, just a bit clumsy. RPG IV has so many ways to do some things that it's easy to overlook an easier method when attempting to do something.
So this week I am presenting some typical questions along with several solutions: one or two clumsy solutions and one or two straightforward solutions. All solutions will work in all situations, but the straightforward solutions are a bit cleaner than the others. Oh, yes, I expect to hear from you in the discussion forum as to why your solution is better than the straightforward solutions I present in this issue. That's what a free exchange of ideas is for, so let's get started.
How Do I Left-Justify Text Within a Character Field?
D End S 5U 0
D Len S 5U 0
D MyValue S 300A Inz(' 01234567')
C ' ' Check myValue Start
C ' ' CheckR myValue End
C eval Len = (End - Start) + 1
C eval myValue = %subst(myValue:Start:Len)
This solution uses a typical application of the %SUBST built-in function to extract just the data within the original MYVALUE field and move it, left-justified, into itself via the EVAL opcode.
Clumsy Solution 2:
D MyValue S 300A Inz(' 01234567')
C ' ' Check myValue Start
C eval myValue = %subst(myValue:Start)
This solution uses the assumption ability of the %SUBST built-in function. If no length parameter is specified, this code assumes you want to use all data in the field, from the starting location through the end of the field. The second solution is much cleaner than the first solution, but it's still a bit clumsy compared to the straightforward solution.
Straightforward Solution:
C eval myValue = %TrimL(myValue)
In this solution, we use the %TRIML (trim left) built-in function to remove left-side blanks from the field's value. Since EVAL copies data left-justified, if we truncate the leading blanks, the combination of %TRIML and EVAL cause the value to be left-justified. Clean, clear, and only one line of code!
How Do I Right-Justify a Value in a Character Field?
Right-justification is a bit more challenging than left-justification. Often, people don't even know the solution exists, let alone how to apply it.
Clumsy Solution:
D Start2 S 5U 0
D End S 5U 0
D Len S 5U 0
D MyValue S 300A Inz(' 01234567')
C ' ' Check myValue Start
C ' ' CheckR myValue End
C eval Len = (End - Start) + 1
C eval Start2 = (%size(myValue) - Len) + 1
C eval %Subst(myValue:Start2) =
C %subst(myValue:Start:Len)
This solution is similar to the left-justify solution, but it uses %SUBST on both the left and right sides of the equals sign (=). By calculating the starting location for the data in the field, you can shift it to the right-justification location, provided you do the math correctly.
Straightforward Solution:
C evalR myValue = %TrimR(myValue)
You knew it had to be that easy, right? The built-in function %TRIMR strips off trailing blanks for the value. Then, the EVALR (EVAL with right-justify) opcode copies the data, right-justified to the target field. So again, with one line of code, you can right-justify data within a field.
How Do I Convert Numeric to Character?
This has been problematic since RPG IV first came out. In RPG III and even in RPG III-style RPG IV code, you can simply use the MOVE opcode to convert between numeric and character; that hasn't changed a bit. But most people ask this question in the context of the EVAL opcode or the so-called "free-format" RPG IV syntax.
Clumsy Solution:
D MyDS DS Inz
D CstNbrA 7A
D CstNbr 7S 0 Overlay(CstNbrA)
D First S 5I 0
D Loc S 5I 0
C eval CstNbr = CustNo
C '0' Check CstNbrA First
C eval Loc = 1
C Dow Loc < First
C eval %Subst(CstNbrA : Loc : 1) = ' '
C eval Loc = Loc + 1
C enddo
In this solution, the packed decimal field is copied into a zoned decimal subfield of the MYDS data structure. The CSTNBR field overlays the CSTNBRA field in that data structure. So when the value is copied from CUSTNO to CSTNBR, the field CSTNBRA contains '0003741'.
The CHECK opcode locates the first non-zero position on the left side of the CSTNBRA subfield. Then, the routine enters a DO loop that replaces the leading zeros with blanks. When the loop exits, the CSTNBRA subfield contains '___3741', which you must then left-justify or do whatever it is you need to do with it.
Straightforward Solution 1:
D CstNbrA S 7A
C eval CstNbrA = %TrimL(%EditC(CustNO:'Z'))
In this solution, the %EDITC built-in function is used to apply the 'Z' edit code to the numeric value in CUSTNO. Since %EDITC returns a character value with leading zeros replaced by blanks (that is, the leading blanks are also returned), I wrap the %EDITC built-in itself in another built-in function, %TRIML, to strip off leading blanks. The result is a left-justified numeric value stored in the CSTNBRA character field, as follows: '3741___'
If you need leading zeros to remain with the value, this second solution may be better for your situation:
Straightforward Solution 2:
D CstNbrA S 7A
C eval CstNbrA = %EditC(CustNO:'X')
In this solution, the lightly documented edit code 'X' is used. The 'X' edit code converts the numeric value to character with leading zeros, and that's all. So you end up with CSTNBRA containing '0003741'.
Straightforward Solution 3:
D CstNbrA S 7A
C eval CstNbrA = %Char(CustNO)
If you prefer a simple solution that doesn't have a lot of thinking behind it, use the %CHAR built-in function. This wonderful built-in function converts numeric values to character and strips off leading zeros, all in one action. The beauty of this built-in function is that it doesn't do anything extra that would need to be managed. If you simply want a numeric value in character format, which is often needed for CGI and EDI applications, %CHAR is the right choice for you.
After the EVAL operation is performed, the CSTNBRA field will contain the value '3741___'.
Suppose you have a field that contains a decimal. For example:
D SalesA S 15A
C eval SalesA = %Char(Sales)
After this solution is performed, the field named SALESA will contain the value '2133.50' plus the trailing blanks, of course. That is, the %CHAR built-in function correctly inserts the decimal notation for the value; it does not insert thousands notation. Also, if the value is negative, a minus sign (-) is embedded for you automatically.
How Do I Convert Character to Numeric?
What about the other way around? What if you need to move a value that is stored in plain text to a numeric field? How do you get that to work?
Clumsy Solution:
D CSTNBRA S 12A Inz(' 3741')
D ZEROS30 S 30A Inz(*ZEROS)
D MyDS DS Inz
D Alpha 30A
D Numeric 30S 0
C evalR Alpha = ZEROS30 + %TrimL(CSTNBRA)
C eval CustNo = Numeric
In this solution, a literal containing 30 zeros is concatenated with the field containing the numeric value. The %TRIML built-in function is used to ensure that no leading blanks are detected. Then, the EVALR (EVAL with right-justify) opcode is used to cause the text to be copied to the ALPHA field, starting with the rightmost characters. That way, the leading zeros from ZEROS30 are truncated, filling the ALPHA field with a neat, zero-filled value. Then, using the NUMERIC subfield that overlays the ALPHA field within the data structure, the numeric value is copied to the packed field.
While I've attempted to make this as least clumsy as possible, it is still quite clumsy.
Straightforward Solution 1:
D CstNbrA S 7A Inz('3741')
D Sales S 11P 2
D SalesA S 7A Inz('1200.32')
C eval CustNo = %int(CstNbrA)
C eval Sales = %Dec(SalesA:11:2)
In this solution, the %INT built-in function is used to convert the plain-text value in the CSTNBRA character field into a numeric value.
This solution uses the %DEC built-in function to convert the plain text to numeric. The primary difference between %DEC and %INT is that %DEC allows the use of decimal places, whereas %INT only works with whole numbers (integers).
One major problem with this solution is that it doesn't appear until V5R2. A year ago, I wouldn't even have shown this solution since virtually no one was even on V5R1 at that point. But today, most people are moving off of OS/400 V4 onto V5R2 (often skipping V5R1 in the process). So I believe it to be a viable solution.
There are a couple of other options as well, however.
Straightforward Solution 2:
D/INCLUDE XTOOLS/QCPYSRC,CHARTONUM
D CustNo S 7P 0
D CstNbrA S 7A Inz('3741')
D Sales S 11P 2
D SalesA S 7A Inz('1200.32')
C eval CustNo = CharToNum(CstNbrA)
C eval Sales = CharToNum(SalesA)
In this solution, the RPG xTools' CHARTONUM procedure is used to convert between character and numeric. CHARTONUM doesn't care what format the input value is in; it can be an integer or a decimal value in plain text, and it converts properly. The benefit of CHARTONUM is that it works as far back as V4R4 and works just as fast as or faster than the built-in functions that are available in V5R2.
Straightforward Solution 3:
D/INCLUDE RPGLAB/QCPYSRC,CPROTOS
D CustNo S 7P 0
D CstNbrA S 7A Inz('3741')
C eval CustNo = atoll(CstNbrA)
This solution is a bit more limited, but it's actually the preferred technique when whole numbers (integers) are being converted. It converts the plain-text numeric value into an integer and then assigns it to the CUSTNO field.
The solution uses the C language runtime library. The atoll function converts a text string containing a number into a numeric integer value. The EVAL copies that value to the target, and you have the conversion. The downside to this technique is that it will not work when decimal values are involved. It only works with whole numbers.
Taking It Easy
The bottom line is that programming with RPG IV can be as complicated as you want to make it, or it can be easy. I choose the easy road and embrace the simple life. I hope you will, too.
Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.
LATEST COMMENTS
MC Press Online