It is important to understand the history behind the RPG language. The language was originally developed in the 1960’s. It was a very high-level language meaning that a lot could be done with few programming statements. It was originally intended to be used as a report writer, hence the name Report Program Generator.
This original form of RPG was unique is several ways: 1 - 5 different record types were used to do everything "F" was for file definitions "I" was for input definitions (record layouts) "C" was for calculations "O" was for output definitions "E" was for exception processing 2 - There were many automatic processing features like the "cycle" The cycle processing automatically read the input file and created the output files… that is, no read or write statements were needed. 3 - Fixed column locations for values of each programming statement. That is, unlike COBOL, C, Java, or Basic, the code is not free form. 4 - The use of Indicators or Switches was painfully required. A switch with a value of '0' is off and a value of '1' is on. Switches were used to determine the results of I-O operations and needed for simple compare instructions. For example, instead of a statement that read like: IF CTR > 0, Do SOMETHING RPG read more like COMPARE CTR to 0 and turn Indicator 90 on if it is > If Indicator 90 is on, Do SOMETHING
The programs were sometimes rather ugly. The use of indicators made many programs very hard to understand.
RPG has evolved nicely to look more and more like other modern languages. While indicators are still used for Input-Output (read and write) operations, standard conditional operations are now available. IF/THEN, DoWhile, Do Until and Select Case operands exist. While still not completely freeform, the statements have portions that are free form. Also, powerful string handling functions like %trim, %subst, %len and %scan are available. If you are familiar the concepts of structured programming you will see that RPG has everything required for structured code.
So, while RPG is about 40 years old, its newest form (RPG IV) is modern and structured. The problem is that many programs are still in use that have very old programming styles. So, you really must be familiar with both RPG/400 (also known as RPG III) and RPG IV (also known as RPG ILE).
RPG is a full featured language that can do all functions of data processing: reading files, randomly retrieving data, navigating through indexes, reading and writing to data terminal or display files, creating print reports and database files.
If you are still concerned that RPG is too old to be useful, consider that Visual Basic and ASP are really just new versions of BASIC. BASIC is as old as RPG. Also, you should know that RPG ILE can create compiled objects that bind with modules written in other languages (like C) and can be used to process CGI scripts on the internet and output dynamic HTML. No kidding.
If you have an RPG book, I suggest you use it in addition to (or in place of) this tutorial. It is a challenging task to describe such a complex language in short, readable topics.
To proceed you need to be familiar with PDM (see topic 4) and should have completed the exercise in topic 5 that copies a file.
Since it important to know the old way of doing things (RPG/400) and the new way (RPG/ILE), I will present each program we write in both languages. Learn the RPG/400 style so you can get a job. Learn the RPG/ILE so you can see how modern the language has become.
Topic 5 showed you how to copy a file named CUST into your user library. Since this will be the input file to our program, make sure that your library list includes your user library. I prefer to have this library as my current library.
So, use DSPLIBL to display your library list. If you don’t see your user library in the list, use the CHGCURLIB USER999 command. This will change your current library to USER999. Remember that when I say USER999, I mean your user ID.
You can look at your data file with the command:
If this doesn’t show your file, either you didn’t copy the file (see Topic 5) or your library list does not include your user library. You should see that the address for every record is “123 MAIN STREET”. We will write a program to read the first record of the file and change the address to “456 OAK”. In the next tutorial we will convert the program to RPG-ILE (RPG IV). In later topics we will change the program to read every record in the file and change the address to “456 OAK” in every record.
We need to enter program source statements, so start PDM (STRPDM). You should see something like:
------------------------------------------------------------------------------------ AS/400 Programming Development Manager (PDM) Select one of the following: 1. Work with libraries 2. Work with objects 3. Work with members 4. Work with projects 5. Work with groups 6. Work with parts 9. Work with user-defined options ---------------------------------------------------------------------------------
Key in 3 and hit ENTER. Fill in the screen to look like:
Specify Members to Work With Type choices, press Enter. File . . . . . . . . . . QRPGSRC Name, F4 for list Library . . . . . . . . user999 *LIBL, *CURLIB, name Member: Name . . . . . . . . . *ALL *ALL, name, *generic* Type . . . . . . . . . *ALL *ALL, type, *generic*, -------------------------------------------------------------------------------
Of course, change the USER999 to your user ID. Hit ENTER now.
If you get an error message that QRPGSRC was not found in USER999, you need to create the file QRPGSRC to hold your RPG-III (RPG/400) source code. For a refresher on this, review the topic on PDM. The command to create QRPGSRC in your library is:
CRTSRCPF USER999/QRPGSRC Your screen should look like:
File . . . . . . QRPGSRC Library . . . . USER999 Position to . . . . . Type options, press Enter. 2=Edit 3=Copy 4=Delete 5=Display 6=Print 7=Rename 8=Display description 9=Save 13=Change text 14=Compile 15=Create module.. Opt Member Type Text (No members in file)
Remember from your PDM lesson that this is trying to show you a list of all of the RPG programs that you have entered. Since you haven’t entered any, there is nothing to show.
You need to add a program. Let’s call the program TUTR001. This name could be any 10 characters but if you don’t follow a naming convention you will confused quickly. This name is derived from TUT (for tutorial), R for RPG and 001.
Hit F6 to add the program. Key in the name (the source member), RPG for the source type and a short description. Your screen should look like:
--------------------------------------------------------------------------- Start Source Entry Utility (STRSEU) Type choices, press Enter. Source file . . . . . . . . . . > QRPGSRC Name, *PRV Library . . . . . . . . . . . > USER000 Name, *LIBL, *CURLIB, Source member . . . . . . . . . tutr001 Name, *PRV, *SELECT Source type . . . . . . . . . . rpg Name, *SAME, BAS, BASP Text 'description' . . . . . . . Change the first record in CUST -----------------------------------------------------------------------
Now hit enter and you will see a blank screen where you can enter RPG source code.
Back in the seventies, programmers had to keep track of what column each operand of the RPG statement was in. It was a mess.
With PDM (actually SEU, the Source Entry Utility), things are easier. Not easy, but easier. The problem is that RPG is a fixed column language. It is not a free format language. SEU provides prompting to help you but even it is a bit complicated at first.
The problem is that the different records types (F, I, E, C, O) do multiple things. That is, the I record type can be used several different ways so it has several different ways of prompting.
The first statement of this program is a FILE specification. That is, it will name the file that we will read and update. Since we want to read the CUST file, we will enter an F record to define the file CUST.
To do this, hit F23. Since your PC keyboard doesn’t have an F23 key, the Telnet5250 product you are using has mapped the SHIFT-F11 key as an F23 key. So, hold down SHIFT and hit F11. The top of your screen should look like:
Select Prompt Type choice, press Enter. Prompt type . . . . . . . . . . . . Values listed below RPG/400: H,F,FC,FK,FX,U,E,L,I,IX,J (I cont),JX,DS,SS,SV,C,O, OD,P (O cont),N,* (Comment)
Wow. This means that there are 21 different formats to choose from. This is an easy one. Since this is a FILE specification, we will use the F prompt. So key F for the Prompt and hit ENTER.
You should now see:
0001.00 ****************** End of data ******************************** Prompt type . . . F Sequence number . . . 0001.00 File File End of File Filename Type Designation File Sequence Format Record Mode of Length of Record Length Processing Key Field Address Type File Overflow Key Field Extn Organization Indicator Start Loc Code Device File File Continuation Exit Entry Addition Condition
What a mess! A couple of things have happened. The first is that the display shows your first line of the program as line 0001.00. Not only that, the SEU editor has highlighted it because it is blank, RPG-III does not allow blank lines! The rest of the screen shows all of the fields that might be used to define a file. Don’t panic yet. All you need to enter is:
CUST for Filename I for File Type F for File Designation E for File Format DISK for Device (2nd line from the bottom on the right)
Hit ENTER and your values will go in the right places. Amazingly, the one value that you can’t enter here is the F that tells RPG that this is a FILE specification. So hit enter, your screen looks like:
FMT F .....FFilenameIPEAF....RlenLK1AIOvKlocEDevice+ *************** Beginning of data ************ 0001.00 CUST IF E DISK ****************** End of data ***************
It also displays an error that “The Type entry is not H, F, I, C ……”
So put your cursor on the character right before CUST and key an F.
Now you have a valid file specification:
FMT F .....FFilenameIPEAF....RlenLK1AIOvKlocEDevice+ *************** Beginning of data ************ 0001.00 FCUST IF E DISK ****************** End of data ***************
Now, calm down. This may be the only time in your life that you actually key in a FILE specification. In the world of programming, everything is copied and changed, cut and paste. Normally, you will simple copy an existing program and then change and add statements to it.
So, what did you just define? You told the program that you will be using the file CUST, that the file is being used for Input (you will read the file), that the file is Fixed length (almost all files are fixed length), that the file is Externally defined (that is, it was defined by the database language of the AS/400) and finally that the file is a DISK file (not a PRINTER or Display Screen).
Imagine for a moment a program where the user enters a customer number on the display screen. The program reads the customer record, looks up the customer’s most recent order and prints it. Such a program will have at least 4 files: Display screen file (a WORKSTN file) Customer file (a DISK file) Order file (a DISK file) Printer file (a PRINTER file)
This is a simple program. It has only one file.
Now we are ready for the processing logic of the program. The total logic of this program will be only 6 statements. Processing logic in RPG is known as CALCULATION statements and uses the C specification and the C prompt type.
Soon, you will get more comfortable with PDM and SEU. Now, we need to add a line to after the F statement. Do this by keying an I (for insert) on top of the 0001.00.
Your screen should look like:
FMT FX .....FFilenameIPEAF........L..I........Device+ *************** Beginning of data ************ I001.00 FCUST IF E DISK
When you hit ENTER, the editor will create a blank line for you and even place your cursor in column 6 where you can enter the C for CALCULATION. With the cursor on the line with the C, hit Shift-F11 to prompt and this time use C for the prompt type.
Your screen will now show:
Prompt type . . . C Sequence number . . . 0002.00 Level N01N02N03 Factor 1 Operation Factor 2 Result Decimal Length Positions H/N/P HI LO EQ Comment
Now finally some good news. Most of the RPG programming you do is using the C specification. This type of statement is not too messy.
The first processing step of the program is to read the first record of the CUST file. RPG needs to know 2 things about a read statement: 1) what file to read 2) what indicator to turn on if there was not a "next" record in the file (that is, what indicator will tell you that there is no more data in the file) Indicators are numbered from 01 to 99. There are some other special ones too. I like to use indicators 90 thru 99 for the read statements. So enter READ for the Operation CUST for FACTOR 1 90 for EQ
Your screen should look like:
0001.00 FCUST IF E DISK 0002.00 C ''''''' ****************** End of data ******************************* Prompt type . . . C Sequence number . . . 0002.00 Level N01N02N03 Factor 1 Operation Factor 2 Result READ CUST Decimal Length Positions H/N/P HI LO EQ Comment 90 When you hit ENTER, it will look like: FMT FX .....FFilenameIPEAF........L..I........Device+......KExit++En *************** Beginning of data *************************** 0001.00 FCUST IF E DISK 0002.00 C READ CUST 90 ****************** End of data ******************************
There are 3 places to enter indicators for statements. Unfortunately, it is not always easy to figure out which place to use. The HI, LO and EQ headings come from the obsolete way indicators were first used. In old RPG, there were no IF statements. Instead, you used a COMP (Compare) statement to compare the values of FACTOR1 and FACTOR2 and then would set on an indicator if FACTOR1 was higher than FACTOR2, a different indicator if FACTOR1 was lower that FACTOR2 and finally an indicator if they were equal. We will NEVER do this.
The lesson here is simple. For a READ instruction, the indicator mentioned in the EQ field will be turned on when there are no more records in the file.
Let’s pause for a minute and look at all that is happening with these 2 statements.
The F specification tells the AS400 to set up a way to read the CUST file. It also tells it to find the fields and attributes of the fields using the database definition of the CUST file. In most other languages, you would need a statement to OPEN the file. Such a statement tells the computer that you are ready to use the file. RPG assumes that you want to read the file and automatically opens the file for you. There is a simple way to tell the AS400 that you want to OPEN and CLOSE the file explicitly but there is no reason to do that.
The READ statement reads the first record of the file CUST and populates an area in your program’s memory with the data that is in the first field. You program has each field defined and now has data in each field.
After the READ, the field CSNBR has a value of 1002, the field CSNAME has a value of E LUMPKIN, the field CSADR1 has a value of 123 MAIN STREET.
We now want to change the value in CSADR1. Unlike other languages, if we move only a few characters to a field that is 30 characters long, RPG will change only the first 3 characters. So we will first blank out the field and then move 456 OAK to the address.
Insert a line for a new C statement. One way to do this is to key an I over the 0002.00 and hit ENTER. Then key C in column 6 and hit Shift-F11. Key C for prompt type and hit ENTER. We want to blank out the CSADR1 field so we will move blanks to it. RPG has some system values defined that are named like: *BLANK or *BLANKS *ZERO or *ZEROES *ON or *OFF *IN90 (for indicator 90)
As you might guess, we want to move the value *BLANK to CSADR1. So fill in your screen to look like:
Prompt type . . . C Sequence number . . . 0003.00 Level N01N02N03 Factor 1 Operation Factor 2 Result MOVE *BLANK CSADR1 Decimal Length Positions H/N/P HI LO EQ Comment
Hit ENTER and your program should look like:
*************** Beginning of data ***************************** 0001.00 FCUST IF E DISK 0002.00 C READ CUST 90 0003.00 C MOVE *BLANK CSADR1 ****************** End of data ******************************** Now insert another line to MOVEL '456 OAK' to CSADR1. This says to move the characters in between the quotes to the field named CSADR1. The operand is MOVEL for MOVE LEFT. This means that since '456 OAK' is only 7 characters and CSADR1 is 30 characters, move '456 OAK' to the left You program should look like: *************** Beginning of data ************************** 0001.00 FCUST IF E DISK 0002.00 C READ CUST 90 0003.00 C MOVE *BLANK CSADR1 0004.00 C MOVE '456 OAK' CSADR1 ****************** End of data ***************************** At this moment, the data on the disk has the value '123 MAIN STREET' in the area of the record for CSADR1. If you ran this program, at this spot in the program, the value of the field CSADR1 in memory would be '123 OAK'. But if you stopped here, the new value ('123 OAK') would not get stored in the file. To do that you must UPDATE the record. In RPG-III verbs or operands can be only 5 characters so the verb is UPDAT. Also, you must use the record name for the file. This is too complicated to explain here but trust me that the record name for the CUST file is CSREC. So add the statement to update the record CSREC. Now that we see that we are using the file for Update (not just Input), it is time to change the I in the FILE definition from I to U. 0001.00 FCUST UF E DISK 0002.00 C READ CUST 90 0003.00 C MOVE *BLANK CSADR1 0004.00 C MOVEL'456 OAK' CSADR1 0005.00 C UPDATCSREC Now all that is left to do is to tell the program to stop. First, we have to discuss a housekeeping item left over from the very early days of RPG. We know that RPG will automatically open the file. However, it will only close the file if the "LAST RECORD" indicator is ON. For now, just make sure the last 2 statements to execute in a program are: MOVE *ON *INLR RETRN The last statement is a RETRN which is an abbreviation of RETURN. So the final program is: 0001.00 FCUST UF E DISK 0002.00 C READ CUST 90 0003.00 C MOVE *BLANK CSADR1 0004.00 C MOVEL'456 OAK' CSADR1 0005.00 C UPDATCSREC 0006.00 C MOVE *ON *INLR 0007.00 C RETRN
Now that you are finished, hit F3 and ENTER to save your statements.
So you now have the source statements to compile an RPG program. You compile it just like you compiled the CL program. Key a 14 on the line next to the program name in PDM and hit ENTER. Like:
Work with Members Using PDM File . . . . . . QRPGSRC Library . . . . USER999 Position to . . . . . Type options, press Enter. 2=Edit 3=Copy 4=Delete 5=Display 6=Print 7=Re 8=Display description 9=Save 13=Change text 14=Compile 15=Cr Opt Member Type Text 14 TUTR001 RPG Change the first record in CUST file
By the way, this program is in the USER000 library. You can see it using PDM.
After compiling your program, you will want to look at the compiled listing. Remember, you do this with the command:
You will see something like:
Work with All Spooled Files Type options, press Enter. 1=Send 2=Change 3=Hold 4=Delete 5=Display 6=Release 8=Attributes 9=Work with printing status Device or Total Opt File User Queue User Data Sts Pages TUTR001 DAVIDMOUNT PRTDGM RDY 5 TUTR001 DAVIDMOUNT PRTDGM RDY 5
Put your cursor next to the last one and key in a 5 and hit ENTER.
You will see your compiler listing. If you key in a B at the top and hit ENTER, the AS400 will take you to the bottom of your listing. If your compile worked, it will look like:
F i n a l S u m m a r y Message Count: (by Severity Number) TOTAL 00 10 20 30 40 50 2 2 0 0 0 0 0 Program Source Totals: Records . . . . . . . . . . : 7 Specifications . . . . . . : 7 Table Records . . . . . . . : 0 Comments . . . . . . . . . : 0 PRM has been called. Program TUTR001 is placed in library USER000. 00 highest severity. * * * * * E N D O F C O M P I L A T I O N * * * *
The phrase “Program TUTR001 is placed in library USER???” means the AS400 understood your statements well enough to crate an executable program.
If your compile didn’t work, start by making sure that you have your user library in your library list. The easy way to do this is:
All that is left is to run the program. There are several ways to do this but the most straightforward way is to use this command:
The program will run almost instantly. Then if you look at the CUST file the address of the first record will have changed:
Boy what a long topic! Each lesson will build on this program. It is important that you get familiar with entering programs, compiling them and looking at the compile listings.
Continue at: http://texas400.com/tutrpglesson1.html
The text above is owned by the site above referred.
Here is only a small part of the article, for more please follow the link