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.
In the following two paragraphs we'll see the operators that work with the REXX type and the use of the PARSE instruction.
These are the operators used in Java compared to the ones of NetRexx:
Java | NetRexx |
Unary on numbers - negation ~ bitwise complement ++ increment -- decrement |
Unary on numbers - negation . . . |
| |
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 . . . . . . |
| |
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 |
| |
Logical && and || or ! not . true true value false false value |
Logical & and | or \ not && xor 1 true value 0 false value |
| |
String operators + concatenation . |
String operators || or abuttal concatenation blanck " with blanck |
| |
Casting and testing (class)object object instanceof class |
Casting and testing class object object<=class or class>=object |
|
PRECEDENCE The operators are now listed in order of precedence:
Java. [] () ++ -- ! ~ instanceof * / % + - << >> >>> < > <= >= == != & ^ | && || ?: = op= , |
NetRexx. [] () + - \ (prefixes) ** * / % // + - blank || abuttal (concatenation) = == > < <= >= << \>> ... & | && = (assignment) |
notes
|
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
|
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
|
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
|
[Index] [Previous Chapter] [Next Chapter]