Tech Tips

October 2017 – Be SELECTive
Do you tend to stack up IF-statements in your CL? You know, you don’t need to do that. Maybe at one time you did but V5R3 brought you the ability to code SELECT statements in the same manner that you can with RPG. Not on V5R3 yet? This might be your excuse to “upgrade”. You start with a SELECT, end with an ENDSELECT, and code your condition testing like you would with an IF-statement.
The following example takes a field that contains a letter of the type of output a user selected. If they typed a “D” then the word “Display” is shown as their choice. If they typed a “P” then “Print” is displayed. Anything else gets the word “ERROR”. Note that the code structure is a bit neater than the CL prompter generates. That’s due to manual editing. It might be a little work but for the readability it’s worth it.

/* Test Display vs. Print */
SELECT
WHEN COND(&OUTPUT = ‘D’) THEN(CHGVAR VAR(&OUTP) +
VALUE(‘Display’))
WHEN COND(&OUTPUT = ‘P’) THEN(CHGVAR VAR(&OUTP) +
VALUE(‘Print’))
OTHERWISE CMD(DO)
CHGVAR VAR(&OUTP) VALUE(‘ERROR’)
CHGVAR VAR(&ERROR) VALUE(‘1’)
ENDDO
ENDSELECT

September 2017 – CL File Pointer Reset
Have you read through a file with a CL program and then wanted to go back to the beginning? As you read through a file you will eventually get to a CPF0864 EOF. What then? There is no SETLL to take you back to the beginning. If you use the CLOSE opcode then the next RCVF will read the first record of the file.

PGM
DCL VAR(&LOOP) TYPE(*LGL) VALUE(‘1’)
DCLF FILE(TESTFILE) OPNID(ID1)
DOWHILE COND(&LOOP)
RCVF OPNID(ID1)
MONMSG MSGID(CPF0864) EXEC(LEAVE)
ENDDO
CLOSE OPNID(ID1)
RCVF OPNID(ID1)
CHGVAR VAR(&ID1_F1) VALUE(&ID1_F1)
ENDPGM

May 2017 – Moving Right Along
This is a continuation from last month regarding the “new” Java-based IBM i emulator that replaces the old “thick client” 5250 emulator. If you are on a green screen with multiple pages (i.e. WRKSPLF, DSPMSG, a sub file, etc.) with multiple screens of information you can now use the scroll wheel on your mouse to page up/down the list. Hey, not all tech tips have to be super techy. This is news you can use!

March 2017 – Custom Orders Don’t Upset Us
Which emulator are you using?  There’s a new Java-based emulator that replaces the old “thick client” 5250 emulator.  Granted, “new” isn’t always “improved”.  When I got mine I soon found that I missed the “fast cursor” of the old emulator.  I had it mapped to my alt-up and alt-down keys.  It allowed me to jump up or down three lines at a time on a green screen.  It was a nice complement to the alt-left and alt-right to jump side to side between text.  Still, I missed it in the new emulator.  …until I noticed the Custom Functions button in the Edit|Preferences|Keyboard… menu option.  Once I clicked the button and then the Add button on the next window, I just gave it the name “Fast Cursor Up” and added [up][up][up] to the Data line.  I then mapped that to the alt-up key by selecting the Category drop-down option of “Custom Functions”, selected my new function, clicked the “assign a key” button, and voila! , I had my fast cursor up again.  Next I repeated the process for the fast down.  It doesn’t have to end there.  Think of the possibilities!  Anything you type over and over you can map to a key.  Basic text you can just type.  System functions go inside brackets and use the SendKeys mnemonics.  For more information on those, click the Help button in the keyboard dialogue box.

February 2017 – Linked Lists
How do handle large amounts of data of unknown size? Did you know that you can allocate memory on the fly and use pointers to create linked lists? Sure you could use arrays but how efficient is that? You have to know up front how big they could possibly be. If you have more data than will fit your program will blow up when you try to go outside of the declared range. If you need to pass the data to another program the system needs to allocate that same amount of memory again and move the data from here to there which also takes time. You could accomplish the same goal by passing a single pointer.

First, you define a data-structure with a pointer:
d Bldg ds based(BldgPtr) likeds(buildings)
d BldgPtr s *
d LastBldg ds based(LastBldgPtr) likeds(buildings)
d LastBldgPtr s *

dcl-ds Buildings qualified template;
Address Char(64);
Address2 char(64);
City char(64);
State char(3);
Zip char(6);
NextBldg pointer;
end-ds;

Next, in your code you define a data-structure with a pointer:
NewBldg = %alloc(%len(Bldg));
FirstBldg = NewBldg;
Bldg.Address = {something};
Bldg.Address2 = {something};
Bldg.City = {something};
Bldg.State = {something};
Bldg.Zip = {something};
Bldg.NextBldg = *null;

To add your next record, save the previous pointer, allocate a new record, and link them.
LastBldgPtr = NewBldgPtr;
NewBldg = %alloc(%len(Bldg));
LastBldg.NextBldg = NewBldg;
{code to populate the rest of this record}

At some point in your process you will need code to free up the memory (for each record):
Dealloc(Bldg);

January 2017 – Free At Last!
Have you used free-format RPG?  Sure, most of us have. For those of you who haven’t you may be interested to learn that we are no longer required to write our code in strict columns.  In other news, mankind has not only invented the wheel but discovered fire as well.  The free-format RPG that we’ve been using has still been limited by the H and F-specs.  Sure, they’ve made those free-format too but where did you start your code …column 7?  Well, guess what?  For over a year now we’ve been able to write code in any column that we wanted, with one caveat.  All you have to do is put the following on line 1, column 1 of your program: **free
That’s it—two asterisks and the word “free”.  After that you can put your code in any column.  Granted, you are limited by the record length as defined by your source file.  RDi won’t care if you go longer but the compiler might. Your coding then may be governed by readability rather than restrictions of the language.  Member attributes remain the same (RPGLE, SQLRPGLE, etc.).  You can still use compiler directives.  You can’t go in and out of free-format with /free and /end-free.  You just need to make sure that everything is coded with proper free-format syntax.  Go ahead, give it a try!  Write a simple program to declare two variables, add them together, and output the result.  As you become more comfortable with it you can try bigger things but this should get you going.

 

November 2016 – Compiler Directives
You’ve probably used /COPY or /INCLUDE to add common code (copy books) to your RPG programs at compile time. Those aren’t the only compiler directives available to you. For example, there is /DEFINE and /UNDEFINE for setting condition names on and off. You can combine these with the /IF, /ELSEIF, /ELSE, and /ENDIF directives. How would you use them, you may ask? One example would be to use them with the code you /COPY or /INCLUDE. The compiler would yell at you if you included c-specs in the d-spec section. Your copy member you could test the conditions so the compiler would know to include it or not.

For example, in the main program you could have code like:
/define ProtoType
/copy CommonStuff

The CommonStuff /COPY member would have the following code:
/if defined(ProtoType)
d BillPolicy pr n
d PolicyNbr 7p 0 value

d FormFound pr n
d FormList * value
d FormId 20a value
d LastItem *
/endif

 

October 2016 3, 2, 1, Launch!:
Here’s an oldie but a goodie… Do you have an interactive job where it would be nice to interact with the outside world? Say like launch a PC application or fire up your web browser and go to a web site? If you’re running System i Access (aka Client Access) it’s a piece of cake.

To launch Notepad this is all that you need in a CL program:
PGM
STRPCO
MONMSG MSGID(IWS4010)
STRPCCMD PCCMD(‘start notepad.exe’) PAUSE(*NO)
ENDPGM
To test this example you can create a simple command source member called notepad with the following line of source, compile it to some place in your library list, and you can type notepad, press ENTER, and notepad will launch.
CMD PROMPT(‘Launch Notepad’)

Launching your default web browser on www.google.com is as simple as:
PGM
STRPCO
MONMSG MSGID(IWS4010)
STRPCCMD PCCMD(‘start http://www.google.com’) PAUSE(*NO)
ENDPGM

 

September 2016 – Who Says That It’s Not Easy Being Green?:
Have you (or do you) work with somebody who will give up green-screen 5250 emulation when they pry it out of their cold, dead, hands? Would you like to introduce them to the wonders of editing programs and performing tasks in RDi? Well, IBM has come up with an option to help—it’s a green-screen within RDI! If you’ve got the latest version of RDi just do the following:

In the “Remote Systems” pane (commonly on the left side of the screen) right-click the system name.
Click on “Host Connection Emulator” in the pop-up window.
If all went well, you should be looking at a green-screen logon screen.

This can also be useful for seasoned RDi users for quick tasks and command line access without leaving RDi.