
Proudly Serving Montana
Montana RPG (Report Program Generator) Overview
RPG (Report Program Generator) is a high-level programming language originally developed by IBM for business applications on mainframe computers. It has evolved over time and is still widely used on IBM midrange systems such as AS/400 (now IBM i) and its successors.
Key Features of RPG:
- Fixed-Format Language: RPG is traditionally written in fixed-format syntax, where columns are used to specify different elements of the program such as operation codes, fields, and indicators.
- Batch Processing: RPG was originally designed for batch processing, particularly for generating reports from large volumes of data. It provides features for defining input files, processing records, and generating output reports.
- Data Description: RPG programs define data structures using data description specifications (DDS). This allows programmers to define fields, data types, record formats, and file structures within the RPG program itself.
- Calculation Specifications: RPG includes calculation specifications where mathematical and logical operations are performed. It supports arithmetic operations, conditional statements, looping constructs, and other control structures.
- Subroutine Support: RPG supports subroutines, allowing programmers to encapsulate reusable logic into separate procedures that can be called from within the main program.
- Integration with Database: RPG integrates closely with database systems, particularly IBM DB2 on IBM i systems. It provides native support for accessing and manipulating database records using embedded SQL statements.
RPG has evolved over the years to support modern programming paradigms and features such as free-format syntax, modular programming, and integration with web services. While its usage has declined in some environments, RPG remains a popular choice for developing business applications on IBM i systems.
File Operations
- OPEN: Opens a database file for input or output.
OPEN FILE(MYFILE) OPTION(*IN:*EXT)
- CLOSE: Closes a previously opened file.
CLOSE FILE(MYFILE)
- READ: Reads a record from an input file.
READ FILE(MYFILE) INTOFLD(&FIELD)
- WRITE: Writes a record to an output file.
WRITE FILE(MYFILE) FROMFLD(&FIELD)
Calculation and Arithmetic
- ADD: Adds two numeric fields.
ADD FLD1(&FIELD1) FLD2(&FIELD2) RESULT(&RESULT)
- SUB: Subtracts one numeric field from another.
SUBTRACT FLD1(&FIELD1) FLD2(&FIELD2) RESULT(&RESULT)
- MULT: Multiplies two numeric fields.
MULT FLD1(&FIELD1) FLD2(&FIELD2) RESULT(&RESULT)
- DIV: Divides one numeric field by another.
DIVIDE FLD1(&FIELD1) BY FLD2(&FIELD2) RESULT(&RESULT)
Conditional Statements
- IF: Performs conditional processing.
IF COND(&FIELD *EQ 'VALUE') THEN(DO)
- ELSE: Specifies an alternative action if the condition is false.
ELSE CMD(DO)
- ENDIF: Ends the IF block.
ENDIF
File Handling
- OPNQRYF: Opens a query file for processing.
- CHAIN: Reads a record from a file based on a key value.
- READ: Reads a record from a file.
- WRITE: Writes a record to a file.
- UPDATE: Updates a record in a file.
- DELETE: Deletes a record from a file.
- SETLL: Positions to a record in a file based on a key value, or sets the file to end-of-file if no such record exists.
- SETGT: Positions to the next greater than or equal record in a file based on a key value.
Calculation and Data Manipulation
- ADD: Adds two numeric values together.
- SUB: Subtracts one numeric value from another.
- MULT: Multiplies two numeric values together.
- DIV: Divides one numeric value by another.
- MOVE: Moves data from one variable to another.
- Z-ADD: Zeroes a variable and then adds a value to it.
- Z-SUB: Zeroes a variable and then subtracts a value from it.
- MOVEL: Moves a character string to a variable with truncation.
Control Structures
- IF: Conditional branching based on a condition.
- DO: Starts a block of code to be repeated a specified number of times or until a condition is met.
- ENDDO: Ends a block of code started by a DO command.
- DOW: Starts a block of code to be repeated until a condition is met.
- ENDDO: Ends a block of code started by a DOW command.
- WHEN: Specifies conditions for executing code within a SELECT structure.
- OTHER: Specifies code to execute when no other conditions in a SELECT structure are met.
Subroutine and Procedure Control
- CALL: Calls another procedure or program.
- EXSR: Executes a subroutine within the same program.
- RETURN: Returns from a subroutine to the calling program.
- CALLB: Calls a bound procedure.
File and Data Definition
- FILE: Declares a file and its associated record format.
- EXFMT: Executes an external file input operation, displaying a formatted screen for user input.
- READP: Reads a record from a file with the intent to update it.
- WRITE: Writes a record to a file.
- DELETE: Deletes a record from a file.
- SETGT: Positions to a record in a file with a key value greater than the specified key value.
File Definition
- FILE: Defines a database file for input or output.
FILE MYFILE RCDFMT(MYRCDFMT) RENAME(MYALIAS)
Data Definition
- DEFINE: Defines program data structures and variables.
DEFINE *FILE MYFILE MYDS
Calculation and Control
- ADD: Adds values together.
ADD FLD1 FLD2 RESULT
- SUB: Subtracts one value from another.
SUB FLD1 FLD2 RESULT
- MULT: Multiplies values together.
MULT FLD1 FLD2 RESULT
- DIV: Divides one value by another.
DIV FLD1 FLD2 RESULT
- MOVE: Moves data from one field to another.
MOVE 'ABC' FIELD
- IF: Conditional branching.
IF FLD1 *EQ 'ABC' AND FLD2 *GT 100
- DO: Begins a loop.
DO *N FLD1 = 1 TO 10 BY 1
- ENDDO: Ends a loop.
ENDDO
Input/Output
- READ: Reads a record from a file.
READ MYFILE
- WRITE: Writes a record to a file.
WRITE MYFILE
Subroutines
- CALL: Calls a subroutine.
CALL MYRPGPGM
- RETURN: Returns from a subroutine.
RETURN
Embedded SQL
- EXEC SQL: Executes SQL statements within RPG.
EXEC SQL SELECT * FROM MYTABLE INTO :MYVAR
In RPG, you can propercase data using built-in functions and techniques. Below is an example of how you can implement propercasing:
RPG Code Example:
H OPTION(*NODEBUGIO:*SRCSTMT) D MyString S 50 INZ('hello world') D ProperString S 50 C eval ProperString = Propercase(MyString) C dsply ProperString
RPG Program Explanation:
- MyString: Input string variable containing the data to be propercased.
- ProperString: Output string variable to store the propercased data.
- Propercase: Built-in function to convert the input string to propercase.
- DSPLY: Display operation code to output the propercased string to the program message queue.
This RPG program takes an input string, 'hello world', and converts it to propercase using the Propercase built-in function. The propercased string is then displayed using the DSPLY operation.
Prompt the User for Input
In RPG, you can prompt the user for input using the Input Specification (INDDS) keyword. Below is an example of how you can implement user input prompting:
H OPTION(*NODEBUGIO:*SRCSTMT) FMYFILE IF E K DISK D UserInput S 10 INZ D Prompt S 50 INZ('Enter your input:') C EXFMT PROMPT C EVAL UserInput = Prompt C UserInput DSPLY
RPG Program Explanation:
- MYFILE: Input file used to define the input operation for prompting.
- UserInput: Variable to store the user input.
- Prompt: Variable containing the prompt message to display to the user.
- EXFMT: Executes the format specified by the input operation for prompting.
- EVAL: Assigns the value entered by the user to the UserInput variable.
- DSPLY: Displays the user input value.
This RPG program prompts the user to enter input using the Prompt variable. It then reads the user input and assigns it to the UserInput variable. Finally, it displays the user input value using the DSPLY operation.
Displaying Data in Row and Column Format in RPG
In RPG, you can display data in row and column format using printer file output with positional control of output fields. Below is an example of how you can implement this:
H OPTION(*NODEBUGIO:*SRCSTMT) FSCREEN CF E WORKSTN FHEADER O E PRINTER OFLIND(*IN03) FDETAIL O E PRINTER OFLIND(*IN03) C 'COLUMN 1' WRITE HEADER C 'COLUMN 2' WRITE HEADER C 'COLUMN 3' WRITE HEADER C '--------' WRITE HEADER C 'Data 1' WRITE DETAIL C 'Data 2' WRITE DETAIL C 'Data 3' WRITE DETAIL
- SCREEN: Workstation file used for screen display.
- HEADER: Printer file used to define the header output format.
- DETAIL: Printer file used to define the detail output format.
- WRITE HEADER: Writes data to the HEADER printer file, representing the column headers.
- WRITE DETAIL: Writes data to the DETAIL printer file, representing the row data.
This RPG program demonstrates how to display data in row and column format. The WRITE operation codes are used to output data to the printer files, which control the layout and positioning of the output on the screen.
Generating Audible Beep Sound in RPG
In RPG, you can generate an audible beep sound using the "Beep" operation code. Below is an example of how you can implement this:
H OPTION(*NODEBUGIO:*SRCSTMT) C BEEP
- Beep: The "Beep" operation code generates an audible beep sound.
This RPG program demonstrates how to generate an audible beep sound using the "Beep" operation code. When this line of code is executed, it produces a beep sound on the system.
Montana Encrypting Data in RPG using CryptoAPI
In RPG, you can encrypt data using the CryptoAPI provided by the Cryptographic Services. Below is an example of how you can implement encryption:
RPG Code Example:
H OPTION(*NODEBUGIO:*SRCSTMT) /COPY QCOPYSRC,P.CRYPTO D PlainText S 100 D EncryptedText S 2000 D Cipher DS LikeDS(QCIPHER) C EVAL PlainText = 'Sensitive data to encrypt' C CALLB 'Qc3EncryptA' PLAINTEXT C PARM EncryptedText C PARM %SIZE(EncryptedText) C PARM Cipher C PARM %SIZE(Cipher) C PARM '*AES ' C PARM '*NONE ' C PARM ' ' C IF Qc3EncryptA_Result = *ZEROS C EXSR DisplayEncryptedData C ELSE C EXSR DisplayError C ENDIF C DisplayEncryptedData... C BEGSR C // Display or use encrypted data C ENDSR C DisplayError... C BEGSR C // Handle error condition C ENDSR
RPG Program Explanation:
- PlainText: Variable containing the data to be encrypted.
- EncryptedText: Variable to store the encrypted data.
- Cipher: Data structure for cryptographic parameters.
- CALLB: Call bound procedure to invoke the Qc3EncryptA API for encryption.
- Qc3EncryptA_Result: Result indicator for API call.
This RPG program demonstrates how to encrypt data using the CryptoAPI provided by the Cryptographic Services. The Qc3EncryptA API is used for encryption, and the encrypted data is stored in the EncryptedText variable.
Montana Displaying a Menu in RPG
In RPG, you can display a menu by creating a display file (DSPF) and using RPG logic to interact with it. Below is an example of how you can implement this:
DSPF Example (MenuDisplay):
A DSPSIZ(24 80 *DS3) A PRINT A MENU01 R TEXT('1. Option 1') A MENU02 R TEXT('2. Option 2') A MENU03 R TEXT('3. Option 3') A MENU04 R TEXT('4. Option 4') A MENU05 R TEXT('5. Exit') A OVERLAY A ERASE
RPG Code Example:
**FREE /include qsysinc/qrpglesrc,mydspf D Option S 1 /free Dow (Option <> '5'); Write MenuDisplay; Read MenuDisplay; If (Menu01 = '1'); // Handle Option 1 ElseIf (Menu02 = '2'); // Handle Option 2 ElseIf (Menu03 = '3'); // Handle Option 3 ElseIf (Menu04 = '4'); // Handle Option 4 EndIf; If (Not %EOF); Leave; EndIf; EndDo; /end-free
Explanation:
- DSPF Example: Defines a display file named MenuDisplay with five options displayed as menu items.
- RPG Code Example: Reads user input from the display file and handles each menu option accordingly.
This RPG program demonstrates how to display a menu by using a display file (DSPF) and RPG logic to interact with it. The program continuously displays the menu and waits for user input. Based on the selected option, appropriate actions are taken. The loop continues until the user chooses to exit.
Need Assistance?
Ever have an idea about a product or service but lack the ability to develop that idea? Are you looking for a reliable person/firm to build your software? Perhaps you are in need of someone to manage projects and teams?
Word of Mouth
[ Latest 10 ]