In addition to simple strings, the
Rexx class supports traditional arrays with a numeric index in square brackets. Of course, NetRexx arrays may be multidimensional. It is not necessary to reserve storage -- simply assign elements as needed.
An even more interesting feature of the
Rexx class is the indexed string, which is similar to a traditional array, but for which the indexes are literal strings rather than numbers (like a Java HashTable). Thus, you might populate an indexed string simply by assigning elements:
method LoadList NameList = ' ' NameList['mickey'] = 'ClubLeader' NameList['minnie'] = 'mouseketeer' NameList['donald'] = 'aDuck'
Indexed strings also behave like arrays in that one may execute a series of instructions on all elements by looping over the indexes. The
loop instruction in Example 2(a) does just this.
Example 2: Two equivalent pieces of code.
During each iteration of the loop, the variable
thisname takes on the value of the next index. For arrays with numeric indexes, the indexes are processed in numerical order. For indexed strings, the order of selection cannot be predicted.
Rexx variables are strings and humans generally perceive even significant portions of program execution in terms of strings, NetRexx includes string manipulation methods. Most of the popular built-in string manipulation functions from Rexx are available as NetRexx methods.
There is also an
exists method to determine if a particular index to an indexed string exists. Using this method, Example 2(a) can be simplified to Example 2(b).
From NetRexx to Java and Back Again
NetRexx uses the Java object model, so Java classes may be invoked directly from NetRexx just as if they were NetRexx classes. The converse is also true.
Listing One is a web-based registration form that must be completed for access to information from the server. (Listing Two displays the applet Registry.)
<html><body bgcolor="#ffffff"> <p> This sample Web page displays the applet Registry. <p> The applet appears centered in the browser window just below this text. <p> <br> <center> <applet code="Registry.class" width=500 height=150> </applet> </center> <p> <br> <font size=2> Last modified: 30 April 1997 </font> </body> </html>
Listing One includes many examples of how you can use Java classes directly:
- It constructs new Java buttons with
bclear = Button("Clear").
- It instantiates editable text fields with
name = TextField(15).
- It creates new Panels with
p1 = Panel().
NetRexx does not need a keyword such as
new to reserve storage for new instances of objects. Nor do you need to explicitly type cast the variable. NetRexx's
stat=Panel() is functionally equivalent to Java's
Panel stat=new Panel().
NetRexx's evaluation of compound terms from left to right is consistent with Java syntax. Invoking Java methods uses the same syntax as invoking NetRexx methods; and our sample is full of examples of using Java methods directly -- in the layout, color-scheme definition, and population of the applet's window and in retrieving (or clearing) user input.
NetRexx provides all the usual assignment, arithmetic, logical, and comparison operators but includes extensions as well. The "equals" sign (=) assignment operator may also be used to assign a type to a variable, as illustrated by
statlbl = Label or
NameList = Rexx. This devolves naturally from the search order for evaluating a term.
A unique operator is blank concatenation, in which a single blank between two terms concatenates their values with a blank in between:
say 'Username:' uname yields
Username: pjt when the value assigned to
uname is "pjt."
NetRexx also uses the blank operator for typecasting as illustrated by
u = Rexx name.getText (at the beginning of the
Register method) where the value of
name.getText is cast to type
Methods Without Madness
method instruction fully supports the concept of a "method signature" as in other languages such as Java. A NetRexx method may also be defined with no signature, in which case it accepts no arguments, and any value it may return is of type
Register methods in our sample share these characteristics.
In the clause
if Register then Status("RegOK"), the value returned from
Register is treated as a Boolean. NetRexx automatically recognizes 0 and 1 (the possible return values) from
Register as Booleans if the context requires, so explicit definition of the return type from
Register is unnecessary. You may wish to do so for clarity, however, as in the
Argument lists are supplied as a list of variables within parentheses immediately adjacent to the method name as illustrated by the
Status methods. For the
action method shown in Example 1, the argument types are explicitly declared. The
Status method shown in Example 3 illustrates the use of optional arguments for which a default value is supplied and, in this case, the default type is
Rexx. Both required and optional arguments may be specified, with the optional ones omitted from the right.
Example 3: The Status method.
Though they are not discussed here, other keywords are available to further qualify a NetRexx method.
Also of Interest
NetRexx supports two styles of comments. Block comments are delimited by pairs of /* and */ and may be nested. The --delimiter begins a line comment which is terminated by the line end.
Unlike Java, NetRexx is generally case-insensitive. This gives you the flexibility to use the capitalization style you prefer. Furthermore, normal comparison operators perform case-insensitive comparisons, freeing you from the problem of case in such operations as comparing user input to expected inputs or specifying method argument strings. In the
Register method, the call
Status("NOUSER") specifies an uppercase argument, but the
Status method compares it to the mixed-case
"NoUser." When case sensitivity is important, the strict comparison operators == and \== can be used.
select construct (used in both the
action method and the
Status method) is the NetRexx equivalent of a
action method (to handle button actions) is present because this sample was implemented for use with JDK 1.02. For JDK 1.1, this would be replaced with appropriate event listeners.
In the real world, the
NameList would be populated from a file or database resident on the server rather than by the
LoadList method. You might use Java classes for this, or you could use the NetRexx RXFile suite developed by Max Marsiglietti, which is accessible through the NetRexx home page.
The actual call to the server to load a restricted URL is commented out in my sample in favor of simply displaying a label that announces a successful connection. Reverse the commenting (and provide an appropriate URL, of course) to make this a live web application.
Pamela is Rexx/Java project manager for the Share Technology Conference, vice-chairman of NCITS Committee J18 for the Rexx Language, and is on the board of directors of the Rexx Language Association. She can be reached at [email protected] stercomm.com.