/plugins/NetRexxScript/tags/0.1.1/atOnce/netrexxAtOnce03.html

# · HTML · 252 lines · 240 code · 12 blank · 0 comment · 0 complexity · ac8c09371be066899ef80903d72e67d3 MD5 · raw file

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html><head><title>NetRexx at Once</title></head>
  3. <body style="color: rgb(0, 0, 0); background-color: rgb(204, 204, 204);" alink="#ff0000" link="#0000ff" vlink="#800080">
  4. <a name="TYPES"></a>
  5. <center>
  6. <h1><font color="#ff0000">NetRexx at Once</font></h1>
  7. <h2><font color="#ff0000">Quick Guide for Java Developers</font></h2>
  8. </center>
  9. <hr><a href="netrexxAtOnce.html">[Index]</a> <a href="netrexxAtOnce02.html">[Previous Chapter]</a> <a href="netrexxAtOnce04.html">[Next Chapter]</a>
  10. <h2><font color="#ff0000">3. Types</font></h2>
  11. <p>To work with NetRexx you need to use only one type: the REXX type.
  12. This is a powerful String class that can be used as a usual String, a
  13. floating number, a whole number and so on. However you can still use
  14. basic types (e.g. int, double etc.) for better performance. A useful
  15. feature is the ability to set the precision of numbers.
  16. </p><p><a name="TYPE REXX"></a>
  17. </p><h3><font color="#ff0000">3.1 NetRexx Strings</font></h3>
  18. <p>In the following two paragraphs we'll see the operators that work with the REXX type and the use of the PARSE instruction.
  19. </p><p><a name="TYPE REXX OP"></a>
  20. </p><h4><font color="#ff0000">3.1.1 Operators</font></h4>
  21. <p>These are the operators used in Java compared to the ones of NetRexx:
  22. </p><p><table border="1" cellpadding="10"><tbody><tr><td bgcolor="#ffffcc">
  23. <b>Java</b></td><td bgcolor="#ccffff"><b>NetRexx</b></td></tr>
  24. <tr><td bgcolor="#ffffcc">
  25. <pre><b>Unary on numbers</b>
  26. - <i>negation</i>
  27. ~ <i>bitwise complement</i>
  28. ++ <i>increment</i>
  29. -- <i>decrement</i>
  30. </pre>
  31. </td><td bgcolor="#ccffff">
  32. <pre><b>Unary on numbers</b>
  33. - <i>negation</i>
  34. .
  35. .
  36. .
  37. </pre>
  38. </td></tr><tr><td colspan="2" bgcolor="#ffccff">
  39. <ul>
  40. <li>No operators working on <b>bitfields</b>.
  41. </li><li>No <b>increment</b> or <b>decrement</b> operators.
  42. </li></ul></td></tr>
  43. <tr><td bgcolor="#ffffcc">
  44. <pre><b>Binary on numbers</b>
  45. + <i>addition</i>
  46. - <i>subtraction</i>
  47. * <i>multiplication</i>
  48. / <i>division</i>
  49. % <i>modulus</i>
  50. &amp; <i>bitwise and</i>
  51. | <i>bitwise or</i>
  52. ^ <i>bitwise xor</i>
  53. &lt;&lt; <i>left shift</i>
  54. &gt;&gt; <i>right shift (sign propagating)</i>
  55. &gt;&gt;&gt; <i>right shift (zero-fill)</i>
  56. op= <i>operator with assignment</i>
  57. </pre>
  58. </td><td bgcolor="#ccffff">
  59. <pre><b>Binary on numbers</b>
  60. + <i>addition</i>
  61. - <i>subtraction</i>
  62. * <i>multiplication</i>
  63. / <i>division</i><br>// <i>remainder</i><br>% <i>integer part of division</i><i></i>
  64. ** <i>power</i>
  65. .
  66. .
  67. .
  68. .
  69. .
  70. .
  71. </pre>
  72. </td></tr><tr><td colspan="2" bgcolor="#ffccff">
  73. <ul>
  74. <li>No operators working on <b>bitfields</b>.
  75. </li><li>Modulus replaced by <b>integer divide</b> and <b>reminder</b>.
  76. </li><li>Added <b>power</b> operator.
  77. </li><li>No operators with <b>assignment implied</b>.
  78. </li></ul></td></tr>
  79. <tr><td bgcolor="#ffffcc">
  80. <pre><b>Relationals</b>
  81. &lt; <i>less than</i>
  82. &gt; <i>greater than</i>
  83. &lt;= <i>less or equal</i>
  84. &gt;= <i>greater or equal</i>
  85. == <i>equal</i>
  86. != <i>not equal</i>
  87. .
  88. .
  89. .
  90. .
  91. .
  92. .
  93. </pre>
  94. </td><td bgcolor="#ccffff">
  95. <pre><b>Relationals</b>
  96. &lt; <i>less than</i>
  97. &gt; <i>greater than</i>
  98. &lt;= \&gt; <i>less or equal</i>
  99. &gt;= \&lt; <i>greater or equal</i>
  100. = <i>equal</i>
  101. \= &gt;&lt; &lt;&gt; <i>not equal</i>
  102. == <i>strictly equal</i>
  103. \== <i>strictly not equal</i>
  104. &lt;&lt; <i>strictly less than</i>
  105. &gt;&gt; <i>strictly greater than</i>
  106. &lt;&lt;= \&gt;&gt; <i>strictly less than or equal to</i>
  107. &gt;&gt;= \&gt;&gt; <i>strictly greater than or equal to</i>
  108. </pre>
  109. </td></tr><tr><td colspan="2" bgcolor="#ffccff">
  110. <ul>
  111. <li><b>\</b> used instead of <b>!</b>
  112. </li><li><b>several combination</b> for the same operator
  113. </li><li>Added <b>strict comparisons</b>, useful to test if two objects are the same.<br>For example "1.2"="1.20" is true, while "1.2"=="1.20" is false<br>and "Leo"="leo" is true, while "Leo"=="leo" is false.
  114. </li></ul></td></tr>
  115. <tr><td bgcolor="#ffffcc">
  116. <pre><b>Logical</b>
  117. &amp;&amp; <i>and</i>
  118. || <i>or</i>
  119. ! <i>not</i>
  120. .
  121. true <i>true value</i>
  122. false <i>false value</i>
  123. </pre>
  124. </td><td bgcolor="#ccffff">
  125. <pre><b>Logical</b>
  126. &amp; <i>and</i>
  127. | <i>or</i>
  128. \ <i>not</i>
  129. &amp;&amp; <i>xor</i>
  130. 1 <i>true value</i>
  131. 0 <i>false value</i>
  132. </pre>
  133. </td></tr><tr><td colspan="2" bgcolor="#ffccff">
  134. <ul>
  135. <li>These operators work on logical values: <b>0</b> stands for false and <b>1</b> for true.
  136. </li></ul></td></tr>
  137. <tr><td bgcolor="#ffffcc">
  138. <pre><b>String operators</b>
  139. + <i>concatenation</i>
  140. .
  141. </pre>
  142. </td><td bgcolor="#ccffff">
  143. <pre><b>String operators</b>
  144. || <i>or</i> abuttal <i>concatenation</i>
  145. blanck <i> " with blanck</i>
  146. </pre>
  147. </td></tr><tr><td colspan="2" bgcolor="#ffccff">
  148. <ul>
  149. <li><b>All</b> the operators in the above sections work on <b>REXX Strings</b>:<br>even the mathematical operators, if the string represents a number.
  150. </li><li>Concatenation of Strings is often <b>implied</b>.<br>For example:<br>'Hi!' 'All.' and 'Hi!' ''All" both mean 'Hi! All.'<br>Finally a||b is 'Hi! All.' if a='Hi! ' and b='All.'
  151. </li></ul></td></tr>
  152. <tr><td bgcolor="#ffffcc">
  153. <pre><b>Casting and testing</b>
  154. (<i>class</i>)<i>object</i>
  155. <i>object</i> instanceof <i>class</i>
  156. </pre>
  157. </td><td bgcolor="#ccffff">
  158. <pre><b>Casting and testing</b>
  159. <i>class</i> <i>object</i>
  160. <i>object</i>&lt;=<i>class</i> <i>or</i> <i>class</i>&gt;=<i>object</i>
  161. </pre>
  162. </td></tr><tr><td colspan="2" bgcolor="#ffccff">
  163. <ul>
  164. <li><b>No parentheses</b> needed to cast an <i>object</i>.
  165. </li><li><b>&lt;=</b> or <b>=&gt;</b> used to test if an <i>object</i> is of type <i>class</i>.
  166. </li></ul></td></tr>
  167. </tbody></table>
  168. </p><p><font color="#ff0000">PRECEDENCE</font> The operators are now listed in order of precedence:
  169. </p><p><table border="1" cellpadding="10"><tbody><tr><td bgcolor="#ffffcc">
  170. <b>Java</b><br>
  171. <pre>. [] ()<br>++ -- ! ~ instanceof<br>* / %<br>+ -<br>&lt;&lt; &gt;&gt; &gt;&gt;&gt;<br>&lt; &gt; &lt;= &gt;=<br>== !=<br>&amp;<br>^<br>|<br>&amp;&amp;<br>||<br>?:<br>= op=<br>,<br></pre>
  172. </td><td bgcolor="#ccffff">
  173. <b>NetRexx</b><br>
  174. <pre>. [] ()<br>+ - \ (<i>prefixes</i>)<br><br>**<br>* / % //<br>+ -<br><br>blank || abuttal (<i>concatenation</i>)<br><br>= == &gt; &lt; &lt;= &gt;= &lt;&lt; \&gt;&gt; ...<br><br>&amp;<br>| &amp;&amp;<br><br>= (<i>assignment</i>)<br></pre>
  175. </td></tr><tr><td colspan="2" bgcolor="#ffccff">
  176. <b>notes</b><br>
  177. <ul>
  178. <li><b>**</b> has less precedence than prefixed <b>-</b>, so -2**2 = 4.
  179. </li></ul>
  180. </td></tr>
  181. </tbody></table>
  182. </p><p><a name="TYPE REXX PARSE"></a>
  183. </p><h4><font color="#ff0000">3.1.2 PARSE Instruction</font></h4>
  184. <p>Parsing is a powerful mechanism that extracts strings from another
  185. string using pattern matching or other rules according to a template.
  186. You can parse strings with the <b>PARSE</b> instruction.<br>For
  187. example, imagine that an application must print on the screen the five
  188. strings passed in the command line as an unique string with commas as
  189. separators. Compare the two chuncks of code:
  190. </p><p><table border="1" cellpadding="10"><tbody><tr><td bgcolor="#ffffcc">
  191. <b>Java</b><br>
  192. <pre>/* This is Split.java */<br>public class split {<br> public static void main(String[] arg) {<br> String retStr = arg[0];<br> String[] string = new String[5];<br> int idx = -1;<br> for(int ct = 0; ct &lt; 4; ct++) {<br> idx++;<br> string[ct] =<br> retStr.substring(<br> idx,<br> idx = retStr.indexOf(",",idx)<br> )<br> ;<br> }<br> string[4] = retStr.substring(idx+1);<br> for(int ct = 0; ct &lt; 5; ct++) {<br> System.out.println(string[ct]);<br> }<br> }<br>}<br></pre>
  193. </td><td bgcolor="#ccffff">
  194. <b>NetRexx</b><br>
  195. <pre>/* This is split.nrx */<br>parse arg a','b','c','d','e<br>say a'\n'b'\n'c'\n'd'\n'e<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br></pre>
  196. </td></tr><tr><td colspan="2" bgcolor="#ffccff">
  197. <b>notes</b><br>
  198. <ul>
  199. <li>The <b>PARSE</b> instruction accepts a string and a template to split it in parts.<br>This instruction avoids a lot of programming for string management.
  200. </li><li>There are several ways to define templates (e.g. you can use<br><b>literal</b> patterns, <b>positional</b> patterns and <b>variable</b> patterns).
  201. </li></ul>
  202. </td></tr>
  203. </tbody></table>
  204. </p><p><a name="TYPE IDX"></a>
  205. </p><h3><font color="#ff0000">3.2 Indexed Strings and Arrays</font></h3>
  206. <p>In NetRexx you can define arrays as in Java. The difference is in the syntax:
  207. </p><p><table border="1" cellpadding="10"><tbody><tr><td bgcolor="#ffffcc">
  208. <b>Java</b><br>
  209. <pre>// Declaring a String Array<br>String[] aString;<br>// Three String Array<br>aString = new String[3];<br>// Initializing<br>String[] aString = {"s1","s2","s3"};<br>// A two dimensinonal Array<br>String[][] bString;<br>// Initializing<br>String[][] bString =<br> {{"s1","s2"},{"s3","s4"}}<br>;<br></pre>
  210. </td><td bgcolor="#ccffff">
  211. <b>NetRexx</b><br>
  212. <pre>-- Declaring a String Array<br>aString=String[]<br>-- Three String Array<br>aString=String[3]<br>-- Initializing<br>aString=['s1','s2','s3']<br>-- A two dimensinonal Array<br>bString=String[,]<br>-- Initializing<br>bString=[['s1','s2'],['s3','s4']]<br>--<br>--<br></pre>
  213. </td></tr><tr><td colspan="2" bgcolor="#ffccff">
  214. <b>notes</b><br>
  215. <ul>
  216. <li>No <b>new</b> keyword required to create arrays.
  217. </li><li>The type of the array with initializers is derived from the <b>first</b> element.
  218. </li></ul>
  219. </td></tr>
  220. </tbody></table>
  221. </p><p>A very powerful feature of NetRexx are <b>Indexed Strings</b>.
  222. Every REXX string may have one or more sub-values that can be retrieved
  223. giving a string index. This sub-values are treated as REXX strings and
  224. they may have sub-values, too.<br>For example, consider a small dictionary from Italian to English. With Java you can use the <b>java.util.Hashtable</b> class:
  225. </p><p><table border="1" cellpadding="10"><tbody><tr><td bgcolor="#ffffcc">
  226. <b>Java</b><br>
  227. <pre>/* This is Translate.java */<br>import java.io.*;<br>import java.util.Hashtable;<br>public class Translate {<br> public static void main(String[] arg) {<br> Hashtable dict = new Hashtable();<br> dict.put("ciao","hi");<br> dict.put("gatto","cat");<br> dict.put("luna","moon");<br>// and more...<br> BufferedReader r =<br> new BufferedReader(<br> new InputStreamReader(System.in)<br> )<br> ;<br> String again;<br> try {<br> do {<br> System.out.print("Translation of ");<br> String trans = r.readLine();<br> if(<br> (trans = (String)dict.get(trans))<br> == null<br> )<br> trans = "unknown"<br> ;<br> System.out.println("is '"+trans+"'");<br> System.out.print("Again? [y/n] ");<br> again = r.readLine();<br> } while(again.equals("y"));<br> } catch(IOException e) {<br> e.printStackTrace();<br> }<br> }<br>}<br></pre>
  228. </td><td bgcolor="#ccffff">
  229. <b>NetRexx</b><br>
  230. <pre>/* This is Translate.nrx */<br>dict='unknown'<br>dict['ciao']='hi'<br>dict['gatto']='cat'<br>dict['luna']='moon'<br>-- and more...<br>again=String<br>loop until again\='y'<br> say'Translation of '<br> trans=ask<br> say'is' dict[trans]<br> say'Again? [y/n] '<br> again=ask<br>end<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br>--<br></pre>
  231. </td></tr><tr><td colspan="2" bgcolor="#ffccff">
  232. <b>notes</b><br>
  233. <ul>
  234. <li>A REXX variable <b>must have a value</b> assigned before it can be indexed.
  235. </li><li><b>Multiple indexing</b> is possible, i.e. every sub-value may have sub-values attached.</li><li>iv1.copyindexed(iv2) returns a merge (union) of two idexed variables iv1 and iv2</li></ul>
  236. </td></tr>
  237. </tbody></table>
  238. </p><p><a href="netrexxAtOnce.html">[Index]</a> <a href="netrexxAtOnce02.html">[Previous Chapter]</a> <a href="netrexxAtOnce04.html">[Next Chapter]</a>
  239. </p><hr><br>
  240. </body></html>