3-Lib banner

The Absolute Beginners Guide to OPL

Part 1 - Your first steps


I won't bore you with any theory at this stage. You'll learn best by getting on and doing some real programming, there's nothing like the satisfaction of saying "Look, I did that!". At some point in the very near future, you will have to start looking at the official Psion OPL manuals and learning a few basic facts but I want to give you some confidence first.... Note that where the instructions differ for each type of Psion, I've colour-coded them to help you pick out the bits relevant for you.

On the Siena, 3a or 3c, go to the system screen and look for the "Program" list. It's usually shown with an "OPL" speech bubble inside an icon of a piece of paper. When you've highlighted it, use the menu command "New file" and enter the name "myfirst" and press Enter. On the Series 5, just tap on "New file" on the toolbar and then enter "myfirst" on the top line of the dialog and choose "Program" on the bottom one. Tap on "OK".

Screen shotYou should see a screen similar to the above. The "PROC" and "ENDP" bits simply mark the start and end of your program (they're short for "PROCedure" and "ENDProcedure" if you really want to know) and you'll notice the cursor is helpfully flashing at the point where you need to start typing something in.

It's very often a good idea to call your programs and files by the same name, if only so you can find things at a later date. Type in "myfirst" at the cursor position (before the colon) and then press the down arrow key to take you 'inside' the procedure. It's here where we can start to do some real work.

For the first few chapters of our tutorial series we're going to write a simple lottery number picker. As you'd imagine, this involves asking the Psion to choose some random numbers for us, so the very first thing we need to do is type the phrase:

RANDOMIZE MINUTE + SECOND

Don't worry too much about what the line actually does, it's just one of the handy lines of code you remember as a programmer to help you on your way. Effectively, it just sets up the Psion's random number generator in a manner which we can use. Try to use the same case (i.e. upper or lower) as me when typing in bits of code, as the distinction between UPPER for OPL commands and lower for variables (the places where we temporarily store our numbers) can make your code much easier to read.

Your screen should look like this:

Screen shot[ A note to people with early ROM 1.00 Series 5s. There is a bug in your OPL interpreter which causes the first random number to always be 0.0004 or some such tiny number. The cure? Get your ROM upgraded for free to 1.01 as soon as possible at the Psion approved upgraders. ]

Now that the random number system is set up, let's try using it. Type in the following lines of code:

PRINT RND
GET

The lines now on screen represent a very simple, but completely working program. The "PRINT" command is a quick way of getting the Psion to print information on the screen. In this case, all we've asked it to print is the result of another special command, "RND". Whenever the Psion sees "RND" it generates a new random number, which is what we want at the end of the day. The "GET" line simply tells the program to stop and get a keypress from you, the user, before carrying on. If we didn't put it in, the program would do its stuff, print out the random number and finish before you had time to blink.

Screen shotTime to run the program. Or rather it's not, as there's something we have to do first. All OPL programs need 'translating' before they can be run. This is because the Psion's processor doesn't understand the english-like OPL commands and so we need to convert the plain text code that you and I can read into something more efficient that is more compatible to the computer. If you press the Menu key and have a browse, you should find the "Translate" command. Use it now and you should very quickly see confirmation that the translation finished properly and the offer to run the program:

Screen shotIf you didn't get this message, and saw an error message flash up instead, chances are you made a typing mistake in the code. Go back and check what you entered against what's shown above.

Anyway, press the "Y" key or tap on "Yes" to run the program. The screen should blank and a small number (in even smaller font size) should be displayed at the top left. You'll immediately notice that our random number is not a whole one and therefore not much good as a lottery suggestion(!) This is because all computers generate random numbers as quanitities between 0 and 1. Don't worry about this too much, though, as it's very easy to make the number more appropriate.

Press any key and you should be back to the main code screen. Move the cursor onto the "PRINT RND" line and change it to:

PRINT INT(RND*50) + 1

Don't be too put off by the fact that all this is starting to look more like higher mathematics than high levels of fun, the line really is extremely simple to understand. As before, the RND bit simply tells the computer to generate a random number (between 0 and 1). As you might guess, the "*50" bit simply means "multiply by 50". So, thinking about it, we now have a random number between 0 and 50. Which is almost what we want for a lottery suggestion.

Two minor tweaks are needed, though. Firstly, the INT() bit around our number tells the Psion to take the INTeger part of the number only, i.e. remove everything after the decimal point, so we'd have a random whole number between 0 and 49 (getting 50 is impossible, as the random number in reality only goes up to 0.999999, to as many digits as you care to count....) Secondly, and obviously, we need to add 1 to the result because lottery ball numbering starts at 1, not 0, giving us a number between 1 and 50. At last! Translate the program as before:

Screen shotAnd say Yes to run it. Up on screen comes a completely random ball selection for the lottery. We're going to stop there for this week, but for the next lesson think about how you might want to add to this very simple start.

Hint: we're going to discover 'loops' and also take a more detailed look at simple variables. I'll also introduce you to comments, which will help make your programs much more readable to the casual eye.


Go to next lesson | Programming index