Here are several introductory Rexx programming examples.
/* find.rex -- file search utility */
parse arg infile textToFind
textToFind = translate( strip( textToFind ) ) /* convert to uppercase */
if textToFind = '' then
call usagemsg 'Please enter the text to find...'
if lines( infile ) = 0 then
call usagemsg 'The input file is empty...'
do lineNumber = 1 for lines( infile )
lin = linein( infile )
if pos( textToFind, translate( lin ) ) > 0 then
say lineNumber'.' lin
end
return 0
usagemsg : procedure
if arg(1) <> '' then do
say 'Note:' arg(1)
say ''
end
say 'Usage:'
say ' rexx find infile textToFind'
exit 1
|
/* directoryListingRevision.rex
revise lines in directory listing so they can be sorted chronologically
usage:
rexx directoryListingRevision < directoryListing > sortableDirectoryListing
assume an input line has the following format:
07-27-01 11:01PM 1237 abc.dat
the resulting output line will be
2001/07/27 23:01 1237 abc.dat
*/
do while lines() > 1
/* acquire a directory listing line, and assign text segments to variables */
parse linein month'-'day'-'year hour':'minute +2 amOrPm size file
/* adjust year to four digit format */
if year > 70 then
year = year + 1900
else
year = year + 2000
/* adjust hour to 24 hour clock format */
if amOrPm = 'PM' & hr <> 12 then
hour = hour + 12
/* emit sortable line */
say year'/'month'/'day hour':'minute right( size, 10 ) file
end
|
/* prepare a sequence of programs to process */
queue 'Program1 -- main program'
queue 'Subprog1 -- subprogram 1'
queue 'Subprog2 -- subprogram 2'
programs = ''
do queued()
parse pull program .
'cc' program'.c'
if rc <> 0 then do
say 'An error occurred while compiling' program'.c ...' rc
exit 1
end
programs = programs program'.obj'
end
'link /out:Module.exe' programs
if rc <> 0 then do
say 'An error occurred while linking Module.exe ... code:' rc
exit 1
end
exit 0
|
/* lottery.rex -- the following generates 6 random lottery numbers */
limit = 49 /* the highest value is 49, adjust as necessary */
numbers = '' /* the selected numbers will be added to this variable */
/* prepare 6 random numbers */
do until words( numbers ) = 6 /* need 6 numbers */
number = random( 1, limit ) /* get another random number */
if wordpos( number, numbers ) = 0 then /* not already selected? */
numbers = numbers number /* remember number */
end /* end loop */
/* format results */
say 'Here come 6 numbers [range: 1-'limit']' /* title line */
say numbers /* show 6 numbers */
say 'Good luck' /* closing remark */
|
/* sieve.rex
Eratosthenes prime number sieve algorithm
*/
arg limit . /* processing limit */
if datatype( limit, 'Wholenumber' ) <> 1 then /* validate numeric value */
call usagemsg 'The limit is not a whole number'
isPrime. = 1 /* presume all numbers are prime ! */
do n=2 to limit
if isPrime.n then
call anotherPrime n
end
exit 0
anotherPrime : procedure expose limit isPrime.
arg prime
say right( prime, length( limit ) ) /* emit current prime */
do multiple=prime by prime to limit /* mark multiples as non-primes */
isPrime.multiple = 0 /* multiples of primes are not prime */
end
return
usagemsg : procedure
if arg(1) <> '' then
say arg(1)
say 'Usage'
say ' rexx sieve limit'
exit 1
|