12
Tue, Nov
6 New Articles

TOP TIPS: RPG September 1998

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

 

Leave That Cursor There!

 

When a user presses the Enter key, the cursor normally moves to the first unprotected input field on the display. In some interactive programs, I want the cursor to remain where it was when the user pressed Enter. To do this, I retrieve the cursor location from positions 370 and 371 of the file’s information data structure (INFDS), and then I transfer the value from the INFDS fields to the fields listed in the cursor location (CSRLOC) parameter of the display file’s DDS.

The DDS and RPG IV fragments in Figure 1 illustrate how this is done. — John Albert
Recreational Equipment, Inc.
This email address is being protected from spambots. You need JavaScript enabled to view it.

 

Use EVAL to Set Adjacent Indicators

 

Q: In RPG III, I can use MOVEA to set a string of adjacent indicators, as in Figure
2. Is there a way to do this using EVAL in RPG IV? — Carolyn Cilla This email address is being protected from spambots. You need JavaScript enabled to view it.

A: If the indicators are contiguous, you can use code like that in Figure 3; as a byproduct, you get named indicators without waiting for V4R2.

— Derek Butland

A: You don’t have to name the individual indicators if you don’t want to. The code in Figure 4 shows how to access all 99 general indicators or some portion thereof.

— Ted Holt Senior Technical Editor Midrange Computing

 

Make Inactive Interactive Programs Timeout

 

Users tend to leave interactive programs running unnecessarily, sometimes causing problems. Another user may need a record that’s locked, or other people may see information to which they’re not authorized.

You can easily make interactive programs timeout. The technique shown here works with both RPG III and RPG IV. I will illustrate with RPG III.

In your display file, code the INVITE keyword at the record level, as in Figure 5. Create the display file, specifying WAITRCD(x), where x is the number of seconds you want to allow the display to remain untouched before timing out.

In your RPG program, add two file continuation lines. One should have the NUM keyword, with a value of 1. (This causes RPG to treat the program as a multiple device file, which is necessary for this technique to work.) The other should have the information data structure (INFDS) keyword and the name of a file information data structure. See Figure 6 for an example.

The file INFDS can have as many subfields as you like, but one of them should be the *STATUS subfield so you can test whether the display file timed out or had some other problem.

Instead of EXFMT, use WRITE to a record format name followed by READ to a file name. In the “Lo” resulting indicator position, specify an indicator that you want to turn on when the display times out.

Follow the READ with a test of the indicator and the *STATUS code. If the display times out, the indicator will turn on and the status code will be set to 1331.

— Ted Holt Senior Technical Editor Midrange Computing

 

How Do You Spell Overflow?

 

RPG programmers are well accustomed to checking for printer overflow in RPG programs. Perhaps the most common method is by coding an indicator in the file description specification of a printer file. Another method, one that works for externally described printer files, is to code an indicator in the “Lo” resulting indicator position of a C-spec, using the WRITE operation.

There’s another way, one that gives more flexibility than either of those methods. Use the printer information data structure (IFNDS). When coding the file specification, specify a file INFDS. In the example in Figure 7, the RPG III program defines printer file PRINT with a file information data structure called PINFDS.

The data structure needs two subfields, one for overflow line and one for current line, as in Figure 8.

This comes in handy with multiple line printing. Suppose you have a group of lines, each one defined as a separate record, that should print together on the same page. If you use either the indicator on the WRITE op code or the indicator on the file specification, you will not know you hit overflow until you hit it. Part of the group might print at the bottom of one page while the rest of the group prints on the top of the next page. Using the printer IFNDS lets you determine how many lines remain until page overflow. If you determine that there is not enough room left on the page to accommodate the entire group, you can print them at the top of the next page instead.

Even when I use this method, I still code an indicator in each printer file specification I use. RPG will hard halt if you reach overflow and do not have an indicator

specified either in the file definition spec or on the WRITE op code. Since there is only one file definition spec and there are many WRITES, I opt for the easier of the two.

— Tim Johnston Technical Editor Midrange Computing

 

Just Right Justification?

 

Q: There is no op code in RPG that right justifies data. For example, if a 4-byte character variable contains the value “123 ”, I want another variable to be “ 123”. Does anyone have a routine they’d care to share with me?

— Joe Koontz This email address is being protected from spambots. You need JavaScript enabled to view it.

A: The best I’ve found so far is in Figure 9, but it’s pretty ugly. — John V. Thompson
Honda New Zealand
This email address is being protected from spambots. You need JavaScript enabled to view it.

A: If you have V4R2, you can use a varying-length temporary variable, as in Figure 10. It’s a bit prettier, and it might perform a tiny bit better if there aren’t many blanks to the left of the right-justified data.

The EVAL sets temp with the value “123” and a length of 3. The MOVE right justifies “123” in var2 and pads on the left with blanks.

—Barbara Morris IBM Toronto RPG Compiler Development

 

Record Locking with READE/READPE

 

Recently, we had a record lock error pop up randomly, and the only thing we could figure was that it was on a READE or READPE statement. After referencing the ILE RPG/400 Reference manual and performing some serious debugging, we discovered what the problem was.

If the file you are performing the READE or READPE on is defined as update, a temporary lock is placed on the next record and compared to the search argument in factor
1. This happens even if the next record does not meet the search criteria. If the record is found not to match the key value, the record-not-found indicator is turned on and the lock is removed.

A simple example shows how this could cause problems. Suppose you have a file that is used to receive items into the warehouse. These items are spread out among different users so that everyone with an RF terminal can share the work. The key to the file is user ID. A user selects the menu option, and the program begins processing READEs on the file, using the current user ID as the search argument. Figure 11 shows a simple example of what this data may look like in the file.

Users Brad Stone (BVSTONE), Chad Wells (CHWELLS), and Rod Larson (RJLARSON) receive items. Suppose Brad Stone begins receiving items a few minutes before Chad. By the time Chad gets to work, Brad is almost done. He is entering in the amount received on his last item (LL-009). Chad begins to receive items and has just read his first record. Before Chad enters the amount received for his first item (BT-873), Brad

completes his last transaction and presses Enter. He gets an error saying that the requested record is in use.

In this case, Chad has a record lock on his first item, and Brad’s job tried to check that record to see if the key matched the search criteria (in this case, his user ID, BVSTONE). Before the program can check the value, it must obtain a temporary lock, but it couldn’t because of Chad’s record lock. So the program crashed.

An easy solution to this problem is to put an error indicator in the “Lo” resulting indicator of the READE statement, as in Figure 12. I use the same indicator that I use to detect EOF so that, if this problem arises, the program treats the error the same as EOF. Another solution is to use the No-Lock option on the READE operation. When you are ready to update, simply chain to the file using the unique key, and process the update. This technique eliminates the need for the temporary lock that the system will try to place on the next record.

—Bradley V. Stone Technical Editor Midrange Computing

 

Don’t Compare to Floating Point Variables

 

We midrange programmers are spoiled. We’ve been using decimal numbers, and everything has always come out nice and precise. Beware of floating point numbers. You can almost bet they won’t be exact. A variable you expect to have the value 5 may be something like 4.99998 or 5.001338 instead.

Convert floating point values to decimal values before comparison. An easy way to convert is with the %DECH (Convert to Packed Decimal Format with Half Adjust) builtin function, as in Figure 13. %DECH requires three arguments: the numeric value to be converted to decimal, the number of digits of the resulting decimal value, and the number of decimal positions in the decimal value.

— Ted Holt Senior Technical Editor Midrange Computing

A R SCREEN

A CSRLOC(ROW COLUMN)

A ROW 3S 0H

A COLUMN 3S 0H

A A 1 B 5 6

A B 1 B 6 6

A C 1 B 7 6

Falber1df cf e workstn infds(WSFeedback)

D WSFeedback ds

D CursorLoc 370 371b 0

C dou a = ‘*’

C exsr pos

C exfmt screen

C enddo

C move *on *inlr

C*****

C pos begsr

C*

C CursorLoc div 256 Row

C mvr Column

C*

C endsr

Figure 1: Making the cursor return to its position on the previous display is easy

C MOVEA’001’ *IN,01

Figure 2: Setting indicators 01, 02, and 03 in RPG III

D Indicators DS BASED(IndPtr)

D iAllowF1 1 1A

D iAllowF2 2 2A

D iAllowF3 3 3A

D iOptionalKeys 1 3A

D IndPtr S * INZ(%ADDR(*IN))

C EVAL iOptionalKeys = *ALL’1’

C EVAL iOptionalKeys = *ALL’0’

C EVAL iOptionalKeys = ‘110’

Figure 3: Setting indicators 01, 02, and 03 in RPG IV

D Indicators S 99 BASED(IndPtr)

D IndPtr S * INZ(%ADDR(*IN))

C EVAL Indicators = *ALL’1’

C EVAL %subst(Indicators:11:9) = ‘111000010’

Figure 4: Accessing all general indicators through EVAL in RPG IV

A DSPSIZ(24 80 *DS3)

A INVITE

A R SCRN01

A 5 5’ENTER A LETTER:’

A LETTER 1 B + 1

Figure 5: The INVITE keyword is necessary for interactive programs that check for timeout

FTIMOUTDFCF E WORKSTN

F KNUM 1

F KINFDS INFDS

IINFDS DS

I *STATUS STATUS

C LETTER DOUEQ*BLANK

C MOVE *BLANK LETTER

C WRITESCRN01

C READ TIMOUTDF 2021

C *IN20 IFEQ *ON

C STATUS IFEQ 1331

C MOVE *BLANK LETTER

C ENDIF

C ENDIF

C ENDDO

C MOVE *ON *INLR

Figure 6: This RPG program allows a display to timeout

FPRINT O E 01 PRINTER KINFDS PINFDS

Figure 7: The traditional method for testing for printer overflow

IPINFDS DS

I B 188 1890CURLIN

I B 367 368OOFLINE

Figure 8: A printer file information data structure defined in RPG III

D var1 s 4

D var2 s 4

C eval var2 = *blanks

C eval % subst(VAR2: % len(VAR2)+1-%len(% trimr(VAR1)))

C = var1

Figure 9: One way to right justify a character string

D var1 s 4

D var2 s 4

D temp s 100a varying

C eval temp = % trimr(var1)

C move (p) temp var2

Figure 10: Another way to right justify a character string

User Item Quantity Quantity ordered received BVSTONE AB-109 10 10 BVSTONE KT-449 4 2 BVSTONE LL-009 19 18 CHWELLS BT-873 5
CHWELLS BU-909 1
RJLARSON FY-310 2
RJLARSON GR-250 25

Figure 11: Example data keyed on user ID

C** HiLoEq

C READEFMT01 5151

Figure 12: An error indicator prevents record lock

D floatvar s 8f

... assume floatvar = 1.0000011

... this test will prove false

C if floatvar = 1.0

... this test will prove true

C if %dech(floatvar:6:2) = 1.0

Figure 13: Compare decimal values instead of floating point 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: