View Single Post
Old 06 July 2015, 16:03   #34
BigFan
Registered User
 
BigFan's Avatar
 
Join Date: Feb 2014
Location: Germany
Posts: 261
Counting words

So we have a list of command and functions, now we need a problem to solve,
a task, or some more examples.

Your task is : Open a file and count the words. Perform the following:

open a filerequester or use command line arguments
open a file
on success process file
display result

ARexx has no built-in GUI. If we want a nifty little requester we can borrow this
from a DOS command.
DOS commands do not have ARexx ports. We cannot communicate directly. We have
to redirect the output and let our program fetch it. Again we use pipe: for this.
Code:
Address command 'requestfile >pipe:a DRAWER SYS: TITLE "Select a text file" NOICONS'
further more you need

If ... Then to deal with results

Open(filehandle, filename) where filehandle is any name which will serve as a
reminder to the opened file and filename is a string with path and name of
file to open

Close(filehandle) to shut the file

Eof(filehandle) to test for end of file

Do While a loop for file processing
...
End

Readln(filehandle) read characters until linefeed

Words(string) counting words in a string in quotes or a variable containing a string
this function has limited capabilities. Everything before a white space is considered being a
word ranging from numbers to letters to signs.

To deal with DOS command "requestfile" we need to prepare the output as it has
additional unwanted quotes. We need to remove those double quotes before using
the string in our functions I suggest to use Compress(string,pattern) to remove
pattern from string.

If you think you can do, stop reading. Try yourself first. Extend your code
with additional line counter.

Here comes a possible solution

Code:
/* simple word and line counter */

pipe = 'pipe:a'
linecount = 0                /* if you want to calc with vars then fill */
count = 0                    /* them first or they remain void symbols*/

Address command 'requestfile >' pipe' DRAWER Sys: TITLE "Select a text file"  NOICONS'

Open(phandle,pipe)
fil0 = Readln(phandle)   /*no error checking required*/
Close(phandle)

fil0 = compress(fil0,'"')   /*a must have, delete unwanted chars*/

If Open(fhandle,fil0) Then Do    /* check if file exists and open it*/
  Do While ~Eof(fhandle)         /* loop to end of file */
    lin0 = Readln(fhandle)       /* naa, i don't comment this */
    linecount = linecount + 1
    count = count + Words(lin0)  /*count words and increase result on every step*/
  End
  Close(fhandle)                  /* we are tidy, not needed but good style*/

  Say "In file "fil0
  Say "Number of words: "count
  Say "number of lines: "linecount - 1
End                                 /* if file cannot be opened we get here*/

Exit                               /*not needed but again good style */
Congratulations for your first rexx program

Last edited by BigFan; 08 July 2015 at 16:59.
BigFan is offline  
 
Page generated in 0.07667 seconds with 11 queries