9 Helpful Functions for Working with Variables in CL on IBM i

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

Ted Holt explains how to use the trim, type-conversion, and case-conversion functions in your IBM i CL programs

Editor's note: This article is excerpted from chapter 4 of Complete CL: Sixth Edition.

This article explains some of the various manipulations you can perform in CL to combine two or more constant or variable values: specifically, the trim, type-conversion, and case-conversion functions.

Trim Functions

Use the three trim functions to retrieve the values of CL character variables with one or more leading or trailing characters removed. These functions do not modify the variable to which they refer. They merely retrieve a value and return it to the invoking operation.

There are three functions in this group:

  • %TRIM: remove leading and trailing characters
  • %TRIML: remove leading (leftmost) characters
  • %TRIMR: remove trailing (rightmost) characters

The trim functions accept two parameters. The first, which is required, is a CL variable from which data is to be copied. The second one contains one or more characters that are to be removed from the value. This parameter is optional and defaults to a blank character.

The example in Figure 1 fuses the %TRIMR function to build a qualified filename from separate file and library names. It removes trailing blanks from the value of the library name, concatenates a slash separator, and adds the simple filename.

9 Helpful Functions for Working with Variables in CL on IBM i - Figure 1

Figure 1: The TRIM function, used to remove undesirable blanks from the last name

Case-Conversion Functions

The case-conversion functions, %UPPER and %LOWER, return the value of a character value in either upper case or lower case. Like other functions, these functions do not modify the variable to which they refer, but return a value.

Figure 2 contains pieces of a batch job stream. Assume that the operator has been asked whether the job should continue or not. To indicate that the job should continue, the operator must enter the word "yes". The CHGVAR command converts the operator's reply to upper case so that the operator can enter the response in all upper case, all lower case, or mixed case. That is, regardless of the case of the operator's response, the IF will not terminate the procedure if the operator enters the word "yes", regardless of case.

9 Helpful Functions for Working with Variables in CL on IBM i - Figure 2

Figure 2: The UPPER function, which eliminates the need to test for lower case and mixed case

It is possible to store data of any character set in a character variable. The %UPPER and %LOWER functions cannot properly capitalize data without knowing which character set the data is stored under. For this reason, these functions permit the CCSID of the data to be specified in a second parameter. The default value of this parameter is zero, which means the data is stored under the job's CCSID.

Type-Conversion Functions

CL has a useful set of functions that convert data from one type to another.

  • %CHAR: convert to character
  • %DEC: convert to packed decimal
  • %INT: convert to signed integer
  • %UINT: convert to unsigned integer

%CHAR returns the character representation of any type of data. It accepts one argument: the value to be converted to character.

The code snippet in Figure 3 illustrates the use of %CHAR to convert a decimal number. This program calls program POST120, which applies some sort of batched updates to a database. POST120 returns the number of updates through packed decimal variable &UpdateCt (update counter). If no updates occurred, there is no need to continue, and control returns to the calling program. If at least one update was applied, this CL procedure uses the SNDPGMMSG (Send Program Message) command to write a message to the job log and continues with other processing.

9 Helpful Functions for Working with Variables in CL on IBM i - Figure 3

Figure 3: The %CHAR function, which converts a decimal data for inclusion in message text

The job log contains a message similar to this one: "Number of updated rows: 105."

The %DEC function converts a value to packed-decimal format. It accepts three parameters - one required and two optional ones:

  • the value to be converted, which may be a character, decimal, signed integer, unsigned integer, or logical value
  • the total number of digits in the decimal value
  • the number of decimal positions

The second and third parameters are optional, but if you specify a value for the second parameter, you must also specify a value for the third parameter.

The default values of the second and third parameters depend on the data type of the first parameter.

  • If the first parameter is defined as *CHAR, the function returns a 15-digit value, with five of those digits assumed to be after the decimal point.
  • If the first parameter is a logical value, the function returns a one-digit value with no decimal positions.
  • If the first parameter is one of the numeric data types, the second and third parameters are assumed to be defined according to the attributes of the first parameter.

If the first parameter is a character value, certain rules apply:

  • Leading and trailing blanks are ignored.
  • Embedded blanks are not allowed.
  • The sign, if specified, may be a leading or trailing plus (+) or minus (-).
  • The decimal point, if specified, may be a period (.) or a comma (,).
  • A blank value is considered to be zero.

In the code in Figure 4, variable &inMaxPages is a parameter that has been passed to this procedure. The %DEC function converts the value of &inMaxPages to a five-digit packed decimal value and stores that value in the &MaxPages variable for use by another program.

9 Helpful Functions for Working with Variables in CL on IBM i - Figure 4

Figure 4: The %DEC function converting to packed decimal format

Even though the CHGVAR command can convert character data to packed decimal, CHGVAR cannot convert a blank value to zero, nor does it permit trailing sign characters. For this reason, it is good to use the %DEC function when converting character values to packed decimal. That is, this:

9 Helpful Functions for Working with Variables in CL on IBM i - This

Not this:

9 Helpful Functions for Working with Variables in CL on IBM i - Not this

The code in Figure 5 uses the %DEC function to convert decimal and character data to suitable formats for the SBMJOB (Submit Job) command.

9 Helpful Functions for Working with Variables in CL on IBM i - Figure 5

Figure 5: The %DEC function converting three values to suitable formats

As shown in Figure 6, RPG program AR100R requires three decimal parameters, all of them whole numbers, defined as five digits, seven digits, and seven digits, respectively.

9 Helpful Functions for Working with Variables in CL on IBM i - Figure 6

Figure 6: RPG program AR100R defining three input parameters

The SBMJOB command cannot pass decimal numbers in those formats, but you can "fool" the system by putting decimal values into character variables.

The variables that begin with &in are the parameters that this program receives from calling programs. Notice that they are defined differently from the way that AR100R requires them to be defined.

The &s variables are character variables that are the size of the decimal values that AR100R requires. That is, the customer number, a five-digit packed-decimal variable, occupies three bytes of storage. The two date variables are seven-digit packed-decimal variables, which occupy four bytes of storage each.

The &d variables are decimal variables that overlay the &s variables in memory. The three CHGVAR commands load the &d variables, and in doing so, change the &s variables. The result is that program AR100R receives the parameters as it defines them.

The %DEC function was not required in any of the three CHGVAR commands, but calling %DEC eliminated the need to check the &in variables for blank values.