MasterClass - April "ARexx isn't a programming language, it's more a way of life". People who say this kind of thing clearly spend way too much time in front of their Amiga and don't get out enough. I firmly believe that ARexx is a programming language, and deserves to be treated as such. Namely, it deserves to be used. What can you do with it? Write programs is the obvious answer, and by golly that's what we'll do. ARexx looks a little like the well-known programming language BASIC, but it is considerably more flexible. Not only does it include many powerful commands, but it can control other programs and be expanded to include support for, as an example, standard Windows and Menus. Last month we saw how to install ARexx, and run a simple program. This month we'll look at some more programming examples to get an idea as to the way in which ARexx works. First of all, let's examine the SAY command as it can be slightly more tricky than you might expect. The best way to discover its secrets is of course to enter the following program and execute it. To do this, create a text file which contains the listing, save it and then use "RX" followed by the filename. Do no simple type this into a Shell, as it won't work. Example listing 1 /* Say examples */ SAY Hello world SAY "Hello World" SAY 'Hello World' SAY "Hello" "World" SAY "Hello"||"World" SAY "'Hello World'" When you run the program with RX you'll see something like this: example1.iff The first thing to note is that the program, like all ARexx programs, starts with a comment. If you leave the comment out the program won't run. Now we come to six different ways of putting text on the screen. The first method uses no form or quotes -- only text. You can see that ARexx displays it as upper case. The subsequent examples use quote makes. You can use either single or double, as long as you are consistant: the first two quoted examples will produce indentical results. You can also see that splitting the words into two and adding quotes to each automatically adds a space. If you don't want a space, use the concatenate command ( || ) which joins the two text strings together without introducing any extra characters. Finally, if you want to include quotation marks, use a different sort -- imagine the outmost pair being removed. With output mastered, we can now learn how to make programs more interactive my getting input from the user. The simplest way to do this is with the PULL command, which waits at the Shell for something to be entered. Example listing 2 /* Pull examples */ SAY "Hello, what's your name?" PULL name SAY "Nice to meet you" name SAY "How old are you?" PULL age IF age > 28 THEN SAY "Wow, that's old..." ELSE SAY "That's not very old." EXIT example2a.iff example2b.iff The first PULL takes in a text string and converts it into upper case (we don't ask for this to happen, it just does). The second PULL takes in a numeric value. This is no distinction between the text and number PULLs as far as we are concerned, it's up to ARexx to keep track of which is text and which is a number. The IF-THEN-ELSE statements do a little processing, and that is all there is to it. Variables We can't go much further without touching on the concept of variables. Variables are locations in the Amiga's memory which store specific values, and are referenced by a special name. For example, when we asked for your age using PULL, ARexx create a variable and stored a number in it. ARexx deals with variables in a very flexible way: when it comes across some text which doesn't mean anything to it, it assume it's a variable. If the variable hasn't been used before (that is, it hasn't been initialised) then it has as a value it's own name in upper case. This is why our very first example, SAY Hello world, did what it did. ARexx thought "Hello" and "World" were to variables and so printed them. As the default contents of a variable is it's own name in upper case, the program display "HELLO WORLD". Now here is an example of how you can make variables which store more than one value. Lets say we need to store the names of five different types of fish, for the start of an excellent Fish Database program. We could use five separate variables, and get the names like this: .. PULL first-fish PULL second-fish PULL third-fish PULL fourth-fish PULL five-fish .. and so on. However, this would make the program rather lengthy. It would also be very hard to expand the program at a later date to include more than five fish. A better way is to use a compound variable -- a variable which has a single base name, but stores different values depending on the associated name appended with a full-stop. Erm... here, look at this: Example listing 3 /* Variable examples */ SAY "Five fish please" DO i = 1 TO 5 PULL fish.i END SAY "Here is the list..." DO i=1 TO 5 SAY fish.i IF fish.i = "HADDOCK" THEN SAY "Hah! I knew you would say Haddock" END EXIT When it is run, you should enter five fishy types. The program automatically creates and assigns variables called "fish.1", "fish.2", "fish.3" and so on because the ".i" is actually the loop counter with itself counts from 1 to 50. Notice the second part of the program goes through the fish names itself, looking for a match. Imagine how hard this would be if there were fifty fish to process... example3.iff Don't think that you have to use numbers to reference the values which a compound variable can store. Here is an interesting little example listing. It creates a minature database of ages for four people, and then asks you to pick a name. Example listing 4 /* More Variable examples */ age. = "Unknown" age.john = 28 age.brian = 15 age.mary = 25 age.anne = 34 SAY "Enter a name, please:" PULL name IF age.name = 'Unknown' THEN SAY "Sorry, I don't know " name ELSE SAY "That person is" age.name "years old" EXIT Of particular interest is the fact that when no extra reference is given, there is an automatic default value present (which we call "Unknown") and this can be tested for. example4.iff Next time... Making use of ARexx with existing programs: creating your own user-defined Macros to do exactly what you want with minimal effort. Box out: On the CDROM ** Lisa there is another LHA file in with this collection, please give it to Mat to un-arc and place on the CDROM ** You will find these listings included on the CDROM in the drawer called (**please ask Mat!**). You will also find some programs from previous ARexx Masterclass features: please read the text file before running them!