Jan 15, 2009

Using free format RPG IV

Basically all you need to add free format source code (calculation specs) to your RPG IV programs is the beginning "/free" and the ending "/end-free".

Here's a code sample used for removing the rightmost (last) hyphen from a string using a do while loop:
c/free
 // find last hyphen
    length = %len(%trim(VarString));
     dow length > 1 and %subst(VarString:length:1)<>'-' ;
      length=length -1 ;
     enddo ;
// remove that hyphen
  if %subst(VarString:length:1)='-' ;
    VarString = %replace('':VarString:length:1) ;
  endif ;
/end-free

Next, an example of CHAIN using file invtable with keys ininv (invoice number) and  invnd (vendor number).  Note that a KLIST is not necessary as defining key fields is much easier in free format RPG.  Don't forget to end each statement with a semicolon.
c/free
   chain(n) (ininv:invnd) invtable;
     eval FndUniq = %found;
/end-free

Similarly an example of SETLL using record format invrtable:
c/free
   setll (ininv:invnd) invrtable;
   reade(n) (ininv:invnd) invrtable;

/end-free

Comments are started with a double forward slash and do not require an asterisk in position seven.

//commenting often increases clarity and, therefore, programmer happiness

Example of calling a program from free format RPG, and passing parms ininv and invnd (invoice number and vendor). Note the Call is implied so it is unnecessary to include it when calling another program:

c/free
mypgm (ininv:invnd);