23
Mon, Dec
1 New Articles

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

CL
Typography
  • 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.

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: