NetRexx at Once

Quick Guide for Java Developers


[Index] [Previous Chapter] [Next Chapter]

3. Types

To work with NetRexx you need to use only a type: the REXX type. This is a powerful String class that can be used as an usual String, a floating number, a whole number and so on. However you can still use basic types (e.g. int, double etc.) for better performances. An useful feature is the possibility to set the precision of numbers.

3.1 NetRexx Strings

In the following two paragraphs we'll see the operators that work with the REXX type and the use of the PARSE instruction.

3.1.1 Operators

These are the operators used in Java compared to the ones of NetRexx:

JavaNetRexx
Unary on numbers
-  negation
~  bitwise complement
++ increment
-- decrement
Unary on numbers
- negation
.
.
.
  • No operators working on bitfields.
  • No increment or decrement operators.
Binary on numbers
+   addition
-   subtraction
*   multiplication
/   division
%   modulus
&   bitwise and
|   bitwise or
^   bitwise xor
<<  left shift
>>  right shift (sign propagating)
>>> right shift (zero-fill)
op= operator with assignment
Binary on numbers
+   addition
-   subtraction
*   multiplication
/   division
%   integer part of division
//  remainder
**  power
.
.
.
.
.
.
  • No operators working on bitfields.
  • Modulus replaced by integer divide and reminder.
  • Added power operator.
  • No operators with assignment implied.
Relationals
<   less than
>   greater than
<=  less or equal
>=  greater or equal
==  equal
!=  not equal
.
.
.
.
.
.
Relationals
<        less than
>        greater than
<= \>    less or equal
>= \<    greater or equal
=        equal
\= >< <> not equal
==       strictly equal
\==      strictly not equal
<<       strictly less than
>>       strictly greater than
<<= \>>  strictly less than or equal to
>>= \>>  strictly greater than or equal to
  • \ used instead of !
  • several combination for the same operator
  • Added strict comparisons, useful to test if two objects are the same.
    For example "1.2"="1.20" is true, while "1.2"=="1.20" is false
    and "Leo"="leo" is true, while "Leo"=="leo" is false.
Logical
&&     and
||     or
!      not
.
true   true value
false  false value
Logical
&   and
|   or
\   not
&&  xor
1   true value
0   false value
  • These operators work on logical values: 0 stands for false and 1 for true.
String operators
+  concatenation
.
String operators
|| or abuttal  concatenation
blanck              "   with blanck
  • All the operators in the above sections work on REXX Strings:
    even the mathematical operators, if the string represents a number.
  • Concatenation of Strings if often implied.
    For example:
    'Hi!' 'All.' and 'Hi! ''All' both mean 'Hi! All.'
    Finally a||b is 'Hi! All.' if a='Hi! ' and b='All.'
Casting and testing
(class)object
object instanceof class
Casting and testing
class object
object<=class or class>=object
  • No parentheses needed to cast an object.
  • <= or => used to test if an object is of type class.

PRECEDENCE The operators are now listed in order of precedence:

Java
. [] ()
++ -- ! ~ instanceof
* / %
+ -
<< >> >>>
< > <= >=
== !=
&
^
|
&&
||
?:
= op=
,
NetRexx
. [] ()
+  -  \ (prefixes)

**
* / % //
+  -

blank || abuttal (concatenation)

=  ==  >  <  <= >=  <<  \>> ...

&
|  &&

= (assignment)
notes
  • ** has less precedence than prefixed -, so -2**2 = 4.

3.1.2 PARSE Instruction

Parsing is a powerful mechanism that extracts strings from another string using pattern matching or other rules according to a template. You can parse strings with the PARSE instruction.
For example, imagine that an application must print on the screen the five strings passed in the command line as an unique string with commas as separators. Compare the two chuncks of code:

Java
/* This is Split.java */
public class split {
  public static void main(String[] arg) {
    String retStr = arg[0];
    String[] string = new String[5];
    int idx = -1;
    for(int ct = 0; ct < 4; ct++) {
      idx++;
      string[ct] =
        retStr.substring(
          idx,
          idx = retStr.indexOf(",",idx)
        )
      ;
    }
    string[4] = retStr.substring(idx+1);
    for(int ct = 0; ct < 5; ct++) {
      System.out.println(string[ct]);
    }
  }
}
NetRexx
/* This is split.nrx */
parse arg a','b','c','d','e
say a'\n'b'\n'c'\n'd'\n'e
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
notes
  • The PARSE instruction accepts a string and a template to split it in parts.
    This instruction avoids a lot of programming for string management.
  • There are several ways to define templates (e.g. you can use
    literal patterns, positional patterns and variable patterns).

3.2 Indexed Strings and Arrays

In NetRexx you can define arrays as in Java. The difference is in the sintax:

Java
// Declaring a String Array
String[] aString;
// Three String Array
aString = new String[3];
// Initializing
String[] aString = {"s1","s2","s3"};
// A two dimensional Array
String[][] bString;
// Initializing
String[][] bString =
  {{"s1","s2"},{"s3","s4"}}
;
NetRexx
-- Declaring a String Array
aString=String[]
-- Three String Array
aString=String[3]
-- Initializing
aString=['s1','s2','s3']
-- A two dimensional Array
bString=String[,]
-- Initializing
bString=[['s1','s2'],['s3','s4']]
--
--
notes
  • No new keyword required to create arrays.
  • The type of the array with initializers is derived from the first element.

A very powerful feature of NetRexx are Indexed Strings. Every REXX string may have one or more sub-values that can be retrieved giving a string index. This sub-values are treated as REXX strings and they may have sub-values, too.
For example, consider a small dictionary from Italian to English. With Java you can use the java.util.Hashtable class:

Java
/* This is Translate.java */
import java.io.*;
import java.util.Hashtable;
public class Translate {
  public static void main(String[] arg) {
    Hashtable dict = new Hashtable();
    dict.put("ciao","hi");
    dict.put("gatto","cat");
    dict.put("luna","moon");
// and more...
    BufferedReader r =
      new BufferedReader(
        new InputStreamReader(System.in)
      )
    ;
    String again;
    try {
    do {
      System.out.print("Translation of ");
      String trans = r.readLine();
      if(
        (trans = (String)dict.get(trans))
        == null
      )
        trans = "unknown"
      ;
      System.out.println("is '"+trans+"'");
      System.out.print("Again? [y/n] ");
      again = r.readLine();
    } while(again.equals("y"));
    } catch(IOException e) {
      e.printStackTrace();
    }
  }
}
NetRexx
/* This is Translate.nrx */
dict='unknown'
dict['ciao']='hi'
dict['gatto']='cat'
dict['luna']='moon'
-- and more...
again=String
loop until again\='y'
  say'Translation of '
  trans=ask
  say'is' dict[trans]
  say'Again? [y/n] '
  again=ask
end
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
notes
  • A REXX variable must have a value assigned before it can be indexed.
  • Multiple indexing is possible, i.e. every sub-value may have sub-values attached.

[Index] [Previous Chapter] [Next Chapter]


TETRACTYS Freeware Main Page hosted by GeoCities Get your own Free Home Page
Hosted by www.Geocities.ws

1