|How To Program In ARexx - Part 2



                           By CROW of The LOC

Have  you  read part 1 of this series, if you have no idea about ARexx I
would  strongly  recommend  that  you do.  In this article I will try to
cover how to create, read and write files and also how to create menus.

With  all  file operations the open function/procedure is used, the open
has the following structure :-

boolean = Open(file, name, [Mode])

file = reference variable for that file.
name = is the path and filename.
mode = what the user wants to do with the file.
Mode values are R = Read (default), W = Write, A = Append.

If everything went ok TRUE is returned otherwise FALSE is returned.


Creating  a  file  -  this example also tells the user if something went
wrong.   If  everything  went  ok,  then  you  should have a file called
tempfile in your RAM:  disk with a size of zero bytes.

/* Example 11 */

If Open("tempfile", "RAM:Tempfile", "W") Then
   Call Close("tempfile"
Else
   Say "Unable To Create File, in RAM:"


Creating and writing to a file - this time more flexible with constants.
NOTE  :-  to  signify  a  block  use  Do then End, (BEGIN END for Pascal
experts!),  if  you  want  more than one statement to be run following a
condition  then  you have to put a Do and an End Around them or you will
get an error or weird results.

/* Example 12 */

fileptr = "tempfile"
path    = "RAM:TempFile"

If Open(fileptr, path, "W") Then Do   /* Here is A DO */
   Call Writeln(fileptr, "This is Line 1")
   Call Writeln(fileptr, "This is Line 2")
   Call Writeln(fileptr, "This is Line 3")
   Call Close(fileptr)
End                                   /* Here is A END */
Else
   Say "Unable to create and write file..."


Opening a file and reading the contents.

/*   Example 13    */

fileptr = "tempfile"
path    = "RAM:TempFile"

If Open(fileptr, path, "R") Then Do
     Do While ~Eof(fileptr)    /* While NOT End Of File */
        Say Readln(fileptr)   /* Read A Line Of Characters */
     End
     Call Close(fileptr)
End
Else
   Say "Unable to open and read file..."


Creating  a  menu  with validation so invalid options are not processed,
also shows how to write procedures.

/* Example 14 */

CR = '0A'x               /* CR = Carriage Return */
FF = '0C'x               /* FF =  FormFeed (Clear Screen) */
ClearScreen = FF || CR   /* || = concatenate : means Join together */

Do Forever
    Say ClearScreen

    Say " (A) - Archive"
    Say " (B) - Batch"
    Say " (C) - Catch"
    Say ""
    Say " (X) - Exit"
    Say ""

    Options Prompt "Enter Choice : "; pull choice 2

    Choice = Upper(Chocie)

    If Choice = "X" Then Do     /* This is where we Quit */
       Say "Exiting..."
       Exit
    End
    Else If Choice = "A" Then Do
       Say "Archive"
       Call WaitForCR()
    End
    Else If Choice = "B" Then Do
       Say "Batch"
       Call WaitForCR()
    End
    Else If Choice = "C" Then Do
       Say "Catch"
       Call WaitForCR()
    End
End

WaitForCR(): Procedure
    Say ""
    Options Prompt "Return to Continue"
    Do Until C==""
       Parse Pull c
    End
Return

NOTE  :   You  may have noticed that when you use the function Say, that
the cursor goes onto the next line this is not very useful when you have
something  like  (run  example  1  if  you  still  do not know what i am
wittering about ) :-

     Say "Enter Your Name : "
     Parse Pull name

So to solve this little quirk, use the following instead :-

     Options Prompt "Enter Your Name : "
     Parse Pull name

Now  all  you have to do is then redefine the prompt to what you want it
do be after this piece of code.

                       Procedures and Functions.

In  this  part  of the article I will try to cover how to create and use
procedures  and  functions.   The  difference  between  a function and a
procedure,  is  that  a  function  is something whereas a procedure does
something.   Please  note  that  this definition does not cover all high
level  language  implementations  of  functions  and procedures.In ARexx
procedure  and functions seem to merge so that you can write a procedure
and function to do the same thing.

This  is  a  very  simple  example of how to define and use a procedure,
variables  defined  in  the  procedure are only visible to the procedure
(there  are  exceptions, that will be covered later).  And variables and
constants defined outside of the procedure are normally not visible to a
procedure.

/* Example 15 */

Call PrettyLine()
Exit

PrettyLine: Procedure

     Say "/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\"

Return


This  example  shows  the  most basic example of passing an actual value
back with the return statement, this is a function :-

/* Example 16 */

Say PI()
Exit

PI:                 /* This Is A Function */
Return 3.14159


Now  lets  show you how to pass values into a procedure, by altering the
PrettyLine  Procedure  in  Example 15 so we can specify type of char and
number of chars to print :-

/* Example 17 */

Say PrintLine("*", 20)     /*  PrintLine( CharString, NoOfChars ) */
Exit


PrintLine:              /* This Is A Function */
       character = arg(1)  /* Get Character To Print */
       number    = arg(2)  /* Get Number Of Chars */
       line      = ""      /* Initialise Line */
       Do Number
          line = line || Character  /* Make Line Of What Ever Char Is */
       End

Return line

The  above  example  shows  how to pass values to a function, it is quit
flexible,  as  a  whole string eg.  "Hello" can be passed and not just a
single  character.   The next example takes this slightly further by now
adding  another parameter so that we now can draw a rectangle instead of
just a single line.

/* Example 18 */

Say PrintLine("*", 20, 10) /* PrintLine(CharString,NoOfChars,NoLines) */
Exit


PrintLine:              /* This Is A Function */

          CR = "0A"x    /* Carriage Return */

    character = arg(1)  /* Get Character To Print */
    number    = arg(2)  /* Get Number Of Chars */
    nolines   = arg(3)  /* Get Number Of Lines */
    line      = ""      /* Initialise Line */
    Do number
       line = line || Character   /* Make Line Of What Ever Char Is */
    End

    box = ""            /* Initialise box */
    Do nolines
       box = box || line || CR
    End

Return box

The  above  example  (18)  uses  the  || symbol which is the concatenate
symbol which is used to join two or more strings together.

end.
