04
Mon, Nov
1 New Articles

RPG Academy: BIF Up Your Code! Start Moving MOVE and MOVEL out of Your Code

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

 

The MOVE and MOVEL op codes are workhorses in fixed-format RPG, but they don't exist in free-format. Use the EVAL op code and a few BIFs to remove MOVE and MOVEL!

 

There are many uses for the MOVE and MOVEL operation codes: initializing (or even declaring) variables, moving values from one variable to another (which sometimes implies a conversion between data typescharacter to numeric or vice versa, for example), truncating a numeric value, copying part of a string to a different string variable, and so on. Providing you with the tools to totally remove MOVE from your code is a considerable task, and I'll try to address it in this and the following TechTips, thus paving the way to a migration to free-format (that too will be the subject of a few TechTips later in this series).

 

Removing MOVE from Your Numeric Conversion Operations

This time, I'll focus on the conversion to numeric data types. Take the example below:

 

D REGION          S              2A   Inz('12')
D REGCOD         S             2P 0 Inz(*ZEROS)

(…)


C                   MOVEL     REGION        REGCOD

 

This is a fairly common use of MOVE: moving a character value into a decimal variable, implicitly forcing a data-type conversion from character to decimal. This works, no question about that. The problem is that the readability is not the best because the MOVEL operation hides the data types involved and might, in some cases, cause unexpected and (more importantly) undetected errors. By using EVAL and the %DEC BIF, as shown below, you can achieve the same result, in a more readable manner that simultaneously prepares the code for a smooth free-format conversion:

 

C                   EVAL     REGCOD = %DEC(REGION)

 

You can add a MONITOR block around the EVAL to check for errors. I won't go into details about that now, but I promise I'll explain it in a future TechTip. Anyway, back to the matter at hand: %DEC can be used with additional parameters that define the precision and number of decimal places used for the conversion operation. These two parameters are optional. There's also another BIF, similar to %DEC, that automatically performs half-adjust; it's %DECH. However, this BIF requires all three parameters (numeric or character expression to convert, precision, and decimal places) to be specified. To help you understand the use of these BIFs and their parameters, here's an example adapted from IBM's ILE RPG Reference Manual:

 

D p7             s             7p 3 inz (1234.567)

D s9             s             9s 5 inz (73.73442)

D f8             s             8f   inz (123.456789)

D c15a           s             15a   inz (' 123.456789 -')

D c15b           s            15a   inz (' + 9 , 8 7 6 ')

D result1         s             15p 5

D result2         s             15p 5

D result3         s             15p 5

 

 

* using numeric parameters    

C                   EVAL     result1 = %DEC(p7) + 0.011

* "result1" is now 1234.57800

C                   EVAL    result2 = %DEC(s9 : 5: 0)

* "result2" is now   73.00000

C                   EVAL    result3 = %DECH(f8: 5: 2)

* "result3" is now 123.46000

 

* using character parameters

C                   EVAL    result1 = %DEC(c15a: 5: 2)

* "result1" is now -123.45

C                   EVAL    result2 = %DECH(c15b: 5: 2)

* "result2" is now 9.88000

 

Have you noticed how the precision parameter in the second and third EVAL operations influences the final result? It's also important to mention that plus sign (+) or minus sign (-) characters will be used when converting a character string to a decimal value, as shown in the fourth EVAL operation. Please read the chapter on Conversion Operations in the ILE RPG Reference Manual for more details.

 

Need to Convert Something to Integer? Here's a (h)%int

What if the target data type was an integer or a float? Well, there are also BIFs for that: %INT and %FLOAT, respectively. Let's start with %INT: it has only one parameterthe character or numeric expression to be converted to integerand it simply drops the decimal places when performing the conversion. In other words, 123.456 will be converted to 123. If you need to take into account the decimal part, performing the half-adjust, then you should use %INTH. It's similar to %INT in every way, except for the half-adjust operation. Here's an example adapted from IBM's ILE RPG Reference Manual:

 
D p7             s             7p 3 inz (1234.567)
D s9             s             9s 5 inz (73.73442)
D f8             s             8f   inz (123.789)
D c15a           s             15a   inz (' 12345.6789 -') 
D c15b           s             15a   inz (' + 9 8 7 . 6 5 4 ') 
D result1         s             15p 5
D result2         s             15p 5
D result3         s             15p 5
 
* using numeric parameters     

C                   EVAL    result1 = %INT(p7) + 0.011

 
 * "result1" is now 1234.01100.
C                   EVAL   result2 = %INT(s9)
 * "result2" is now   73.00000
C                   EVAL    result3 = %INTH(f8)
 * "result3" is now 124.00000.
 
 * using character parameters 
C                   EVAL    result1 = %INT(c15a)
 * "result1" is now -12345.00000 
C                   EVAL    result2 = %INTH(c15b)
 * "result2" is now 988.00000 

 

The second and third EVAL operations show the difference between %INT and %INTH: if the example used %INT instead of %INTH in the third EVAL operation, the result3 variable would contain 123.0000 instead of 124.00000.

 

Whatever %FLOAT(s) Your Boat

Finally, a quick word about %FLOAT: it works just like %INT, and it converts the numeric or character expression to float. If the value to convert is a character expression, the following rules apply:

  • The sign is optional. It can be a plus sign (+) or a minus sign (-), but it must precede the numeric data.
  • The decimal point is optional, and you can use either a period or a comma.
  • If invalid numeric data is found, an exception occurs with status code 105.
  • The exponent is optional. It can be either E or e. The sign for the exponent is optional, and it must precede the numeric part of the exponent.
  • Blanks are allowed anywhere in the data. For example, + 3 , 5 E 9 is a valid parameter.

 

The first three rules are also applicable to %INT and %INTH. Just to consolidate all this information, here's an example adapted from IBM's ILE RPG Reference Manual:

 

D p1             s             15p 0 inz (1)
D p2              s             25p13 inz (3)
D c15a           s             15a   inz('-5.2e-1')
D c15b           s             15a   inz(' + 5 . 2 ')
D result1         s             15p 5
D result2         s             15p 5
D result3         s             15p 5
D result4         s             8f
 
 * using numeric parameters
C                   EVAL    result1 = p1 / p2
 * "result1" is now 0.33000.
C                   EVAL    result2 = %float (p1) / p2
 * "result2" is now 0.33333.
C                   EVAL    result3 = %float (p1 / p2)
 * "result3" is now 0.33333.
C                   EVAL    result4 = %float (12345)
 * "result4" is now 1.2345E4 
 
 * using character parameters 
C                   EVAL    result1 = %float (c15a)
 * "result1" is now -0.52000.
C                   EVAL    result2 = %float (c15b)
 * "result2" is now 5.20000.
C                   EVAL    result4 = %float (c15b)
 * "result4" is now 5.2E0

 

A Word About Fixed-Format Code

All the examples of this and the next TechTips were "downgraded" from free-format to fixed-format to keep the readers unfamiliar with "modern" RPG in the loop. But don't get me wrong: I don't advise writing new code in fixed-format! However, migrating to free-format can be a somewhat complicated process (especially inside our heads), and I'll get to that after preparing the ground with a few more tips that (I hope) will help you make a smoother transition from "old" to "modern" RPG.

 

The next TechTip will continue on this path, covering a few string-related BIFs as well as the conversion from numeric to character data types, which is another big use of MOVE and MOVEL. I want to hear from you: reader feedback is very important for writers! Comment, argue, agree, or disagreejust speak your mind in the Comments section below!

Rafael Victoria-Pereira

Rafael Victória-Pereira has more than 20 years of IBM i experience as a programmer, analyst, and manager. Over that period, he has been an active voice in the IBM i community, encouraging and helping programmers transition to ILE and free-format RPG. Rafael has written more than 100 technical articles about topics ranging from interfaces (the topic for his first book, Flexible Input, Dazzling Output with IBM i) to modern RPG and SQL in his popular RPG Academy and SQL 101 series on mcpressonline.com and in his books Evolve Your RPG Coding and SQL for IBM i: A Database Modernization Guide. Rafael writes in an easy-to-read, practical style that is highly popular with his audience of IBM technology professionals.

Rafael is the Deputy IT Director - Infrastructures and Services at the Luis Simões Group in Portugal. His areas of expertise include programming in the IBM i native languages (RPG, CL, and DB2 SQL) and in "modern" programming languages, such as Java, C#, and Python, as well as project management and consultancy.


MC Press books written by Rafael Victória-Pereira available now on the MC Press Bookstore.

Evolve Your RPG Coding: Move from OPM to ILE...and Beyond Evolve Your RPG Coding: Move from OPM to ILE...and Beyond
Transition to modern RPG programming with this step-by-step guide through ILE and free-format RPG, SQL, and modernization techniques.
List Price $79.95

Now On Sale

Flexible Input, Dazzling Output with IBM i Flexible Input, Dazzling Output with IBM i
Uncover easier, more flexible ways to get data into your system, plus some methods for exporting and presenting the vital business data it contains.
List Price $79.95

Now On Sale

SQL for IBM i: A Database Modernization Guide SQL for IBM i: A Database Modernization Guide
Learn how to use SQL’s capabilities to modernize and enhance your IBM i database.
List Price $79.95

Now On Sale

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: