Quick And Handy RPG Output
March 12, 2014 Ted Holt
Are you one of those people who thrive on complexity? Do you love bureaucracy and paperwork? If so, you’re reading the wrong newsletter. I like everything to be as simple as possible. Today I have a simple routine that is a time-saver for me. I hope you find it useful.
My little routine lets me print anything with minimal effort. I can print character variables, numeric variables, data structures, the results of expressions, and anything else that can be assigned to a character variable. I developed it because I needed a quick way to add output to RPG programs without the effort of writing DDS and O specs.
I used various names for this and similar routines, but the name I finally settled on is writeln, pronounced “write line.” If you know anything about Pascal (the programming language, not the scientist), you know that I took the name from Pascal’s primary stream output procedure.
Here’s the source code.
Fqsysprt o f 132 printer usropn D writeln pr D inString 132a const P writeln b D pi D inString 132a const // locals D ReportLine ds 132 /free ReportLine = inString; write qsysprt ReportLine; /end-free P e
There’s not much to it, huh? Use any program-described printer file you like. I used QSYSPRT for illustration.
Writeln prints anything you throw at it, within reason. Here are some examples:
1. Print a character variable.
if ErrorMessage <> *blanks; writeln(ErrorMessage); endif;
2. Print a character literal.
writeln('** End **');
3. Print a numeric value.
writeln('valuelen=' + %char(vallen));
4. Print an expression.
writeln(%trim(zSqlData.SerialNbr) + Sep + FmtDate(zSqlData.InstallDate) + Sep + %trimr(zSqlData.PartNbr) + Sep + FmtStr(zSqlData.PartDesc) + Sep + FmtDate(zSqlData.EntryDate) + Sep + %trim(zSqlData.Model) + Sep + FmtStr(zSqlData.CompanyName) + Sep + FmtStr(zSqlData.CompanyCityState) + Sep + FmtZip(zSqlData.CompanyZip));
Why has writeln been helpful?
1. It’s quick. I don’t have to write DDS or O specs.
2. It’s easily discarded. I might use writeln during development or debugging, then delete it from the program or subordinate it to a disabled compiler directive before installing into production. Since I have so little time invested in the calls to my routine, I don’t feel as if I’ve wasted time.
3. It’s easily replaced. I sometimes use writeln while developing in order to concentrate on the logic of a program. When I have the logic working the way I want it to, I turn my attention to formatting, which means my DDS tends to come out the way I want it without a lot of experimentation and revision.
4. It’s good for free-format output. I’ve even used this routine to build CSV files.
That should be enough to get you going, and hopefully enough to whet your appetite.