Per utilizzare il NetRexx è necessario utilizzare un solo tipo: il tipo REXX. Si tratta di una potente classe stringa che può essere usata come le normali stringhe, un numero in virgola mobile, un numero intero e così via. E' comunque possibile definire ancora i tipi fondamentali (ad esempio int, double etc.) per ottenere delle prestazioni migliori. Una caratteristica utile è quella di poter scegliere la precisione dei numeri utilizzati.
Nei due paragrafi successivi vedremo gli operatori che agiscono sul tipo REXX e l'uso dell'istruzione PARSE.
Questi sono gli operatori definiti in Java confrontati con quelli del 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 |
| |
PRECEDENZA Gli operatori sono ora elencati in ordine di precedenza:
Java. [] () ++ -- ! ~ instanceof * / % + - << >> >>> < > <= >= == != & ^ | && || ?: = op= , |
NetRexx. [] () + - \ (prefixes) ** * / % // + - blank || abuttal (concatenation) = == > < <= >= << \>> ... & | && = (assignment) |
nota
| |
Il parsing è un potente meccanismo che estrae stringhe da un'altra stringa usando il pattern matching o altre regole secondo un template. Per questo scopo può essere utilizzata l'istruzione PARSE.
Per esempio, immaginiamo che un'applicazione debba stampare sullo schermo cinque stringhe che sono inserite come un'unica stringa (ma separate da virgole) nella linea di comando:
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 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
note
| |
In NetRexx si possono definire Array come in Java. L'unica differenza è nella sintassi:
Java// Declaring a String Array
String[] aString;
// Three String Array
aString = new String[3];
// Initializing
String[] aString = {"s1","s2","s3"};
// A two dimensinonal 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 dimensinonal Array bString=String[,] -- Initializing bString=[['s1','s2'],['s3','s4']] -- -- |
note
| |
Una caratteristica molto potente del NetRexx sono le stringhe indicizzate. Ogni stringa REXX può avere uno o più sotto-valori che possono essere recuperati usando un indice stringa. Questi sotto-valori sono trattati come stringhe REXX e quindi possono avere a loro volta dei sotto-valori.
Per esempio, consideriamo un piccolo dizionario dall'italiano all'inglese. Con Java è possibile utilizzare la classe java.util.Hashtable:
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 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
note
| |
[Indice] [Capitolo precedente] [Capitolo successivo]
hosted by