Rexx: Difference between revisions
imported>Paul Wormer No edit summary |
imported>Paul Wormer No edit summary |
||
Line 6: | Line 6: | ||
Features of the language are: easy to learn, [[procedure (computers)|procedural]], [[interpretation (computers)|interpreted]], [[variable typing|untyped]] (or rather one type: character string of arbitrary length), support of numbers of arbitrary precision, [[associative array]]s, powerful character string parsing, invokable from different environments, passing commands to its invoking environments. | Features of the language are: easy to learn, [[procedure (computers)|procedural]], [[interpretation (computers)|interpreted]], [[variable typing|untyped]] (or rather one type: character string of arbitrary length), support of numbers of arbitrary precision, [[associative array]]s, powerful character string parsing, invokable from different environments, passing commands to its invoking environments. | ||
There are text editors, notably IBM's XEDIT<ref>XEDIT User's Guide. (2d ed., Dec. 2005, pub. no. SC24-6132-01). International Business Machines</ref>, Mansfield's KEDIT<ref>[http://www.kedit.com/ KEDIT (commercial)]</ref>, and Hessling's THE<ref>[http://hessling-editor.sourceforge.net/ THE (freeware)]</ref>, that are integrated with Rexx. | There are text editors, notably IBM's XEDIT<ref>XEDIT User's Guide. (2d ed., Dec. 2005, pub. no. SC24-6132-01). International Business Machines</ref>, Mansfield's KEDIT<ref>[http://www.kedit.com/ KEDIT (commercial)]</ref>, and Hessling's THE<ref>[http://hessling-editor.sourceforge.net/ THE (freeware)]</ref>, that are integrated with Rexx. Such integrated pairs form a powerful tool: from the editor a Rexx macro may be invoked and from the Rexx macro editor statements may be issued. This feature allows an almost infinite extension and adaptation of a text editor to one's personal needs. Word processors, such as Word<sup>®</sup> and WordPerfect<sup>®</sup>, are usually to be preferred for natural language texts—except for markup languages like [[HTML]] and [[LaTeX]]—but for editing of computer programs and many kinds of data files, text editors (plus programmable extensions like Rexx) are a very good option. | ||
==Sample macro== | |||
To give the flavor of Rexx, a sample editor macro is presented. It is assumed that the editor acts on a data file consisting of lines of equal length. The data file is assumed sorted so that duplicate lines are consecutive. The macro finds the duplicate lines and deletes them. The macro is invoked from the editor command line with two numeric arguments, col1 and col2, which means that only the character string between columns col1 and col2 is checked for duplication. | |||
<pre> | |||
parse arg LeftCol RightCol /* parse arguments (column numbers)*/ | |||
if LeftCol = "" & RightCol = "" then do /* No column numbers */ | |||
/* Use default columns */ | |||
LeftCol = 1 | |||
RightCol = 80 | |||
end | |||
if datatype(LeftCol) \= "NUM" | datatype(RightCol) \= "NUM" then do | |||
/* (Rexx has function datatype, returns "NUM" or "CHAR") */ | |||
'emsg DELDUP Error 1: Invalid columns specified "'LeftCol RightCol'"' | |||
/* (Command between quotes is passed back to calling environment, */ | |||
/* "emsg" is editor command [error message]) */ | |||
exit 1 /* Leave macro plus error code */ | |||
end | |||
if RightCol > width.1() then do /* width is editor function */ | |||
RightCol = width.1() | |||
say "Specified right column > WIDTH setting... adjusted to" RightCol | |||
end | |||
if LeftCol > RightCol then do | |||
'emsg DELDUP Error 2: Specified left column > right column "'LeftCol RightCol'"' | |||
exit 1 | |||
end | |||
"preserve" /* Save all editor settings, such as automatic save */ | |||
"autosave off" | |||
"top" /* Editor command: top of file */ | |||
NumDups = 0 | |||
/* Loop to process all lines of the file */ | |||
do forever | |||
CompareLine = substr(curline.3(), LeftCol, RightCol-LeftCol+1) | |||
/* curline is editor function, returns current line; substr is Rexx function */ | |||
"down 1" /* Editor command; next line */ | |||
/* Loop to delete all duplicates of one particular line */ | |||
do forever | |||
/* Have we hit the end of the file? */ | |||
if focuseof() then leave /* Editor function */ | |||
/* Is this a duplicate? */ | |||
if CompareLine \== substr(curline.3(), LeftCol, RightCol-LeftCol+1) then leave | |||
"delete 1" /* Editor command, delete one line */ | |||
NumDups = NumDups + 1 /* Count number of duplicates */ | |||
end | |||
if focuseof() then leave /* If current line end-of-file, leave loop */ | |||
end | |||
"top" | |||
"restore" /* All editor settings restored */ | |||
say NumDups "duplicate lines have been deleted..." | |||
exit 0 | |||
</pre> | |||
==References== | ==References== | ||
<references /> | <references /> |
Revision as of 05:23, 25 November 2009
Rexx (REstructured eXtended eXecutor) is a high-level computer programming language developed between 1979 and 1982 by M. F. Cowlishaw. Although Cowlishaw, who is an IBM employee, developed the language in his spare time, IBM soon recognized the usefulness of the language and introduced it in 1982 into the operating system VM/CMS as its main scripting language. Around the same time IBM's CMS editor, XEDIT, was extended so that edit macros could be written in Rexx.
At present IBM includes support for the language in almost all its operating systems. Non-IBM freeware versions are available for MS-Windows, most variants of Unix, and Mac OS X.
Features of the language are: easy to learn, procedural, interpreted, untyped (or rather one type: character string of arbitrary length), support of numbers of arbitrary precision, associative arrays, powerful character string parsing, invokable from different environments, passing commands to its invoking environments.
There are text editors, notably IBM's XEDIT[1], Mansfield's KEDIT[2], and Hessling's THE[3], that are integrated with Rexx. Such integrated pairs form a powerful tool: from the editor a Rexx macro may be invoked and from the Rexx macro editor statements may be issued. This feature allows an almost infinite extension and adaptation of a text editor to one's personal needs. Word processors, such as Word® and WordPerfect®, are usually to be preferred for natural language texts—except for markup languages like HTML and LaTeX—but for editing of computer programs and many kinds of data files, text editors (plus programmable extensions like Rexx) are a very good option.
Sample macro
To give the flavor of Rexx, a sample editor macro is presented. It is assumed that the editor acts on a data file consisting of lines of equal length. The data file is assumed sorted so that duplicate lines are consecutive. The macro finds the duplicate lines and deletes them. The macro is invoked from the editor command line with two numeric arguments, col1 and col2, which means that only the character string between columns col1 and col2 is checked for duplication.
parse arg LeftCol RightCol /* parse arguments (column numbers)*/ if LeftCol = "" & RightCol = "" then do /* No column numbers */ /* Use default columns */ LeftCol = 1 RightCol = 80 end if datatype(LeftCol) \= "NUM" | datatype(RightCol) \= "NUM" then do /* (Rexx has function datatype, returns "NUM" or "CHAR") */ 'emsg DELDUP Error 1: Invalid columns specified "'LeftCol RightCol'"' /* (Command between quotes is passed back to calling environment, */ /* "emsg" is editor command [error message]) */ exit 1 /* Leave macro plus error code */ end if RightCol > width.1() then do /* width is editor function */ RightCol = width.1() say "Specified right column > WIDTH setting... adjusted to" RightCol end if LeftCol > RightCol then do 'emsg DELDUP Error 2: Specified left column > right column "'LeftCol RightCol'"' exit 1 end "preserve" /* Save all editor settings, such as automatic save */ "autosave off" "top" /* Editor command: top of file */ NumDups = 0 /* Loop to process all lines of the file */ do forever CompareLine = substr(curline.3(), LeftCol, RightCol-LeftCol+1) /* curline is editor function, returns current line; substr is Rexx function */ "down 1" /* Editor command; next line */ /* Loop to delete all duplicates of one particular line */ do forever /* Have we hit the end of the file? */ if focuseof() then leave /* Editor function */ /* Is this a duplicate? */ if CompareLine \== substr(curline.3(), LeftCol, RightCol-LeftCol+1) then leave "delete 1" /* Editor command, delete one line */ NumDups = NumDups + 1 /* Count number of duplicates */ end if focuseof() then leave /* If current line end-of-file, leave loop */ end "top" "restore" /* All editor settings restored */ say NumDups "duplicate lines have been deleted..." exit 0
References
- ↑ XEDIT User's Guide. (2d ed., Dec. 2005, pub. no. SC24-6132-01). International Business Machines
- ↑ KEDIT (commercial)
- ↑ THE (freeware)