Rexx Programming/How to Rexx/datatype
There is, in principle, only one data type in Rexx: the character string. Strings represent letters, numerals, spaces, punctuation marks, and other kinds of text stored in the computers memory. In practice, Rexx recognizes several different forms of strings and can use them in different ways. So we can speak of different data types within the overall string type.
Strings
[edit | edit source]Strings are sequences of text characters. They can be stored in variables, generated within a program, or directly written into the code using single or double quotation marks. Uninitialized variables are interpreted as uppercase versions of their own names.
say hello /* unitialized variable - says HELLO */ say 'WORLD' /* single quote string - says WORLD */ say "123" /* double quote string - says 123 */ hello = '---' /* initialized variable - now contains 3 dashes */ say hello /* outputs the 3 dashes */ hello = "" /* the empty string - yes, that's a thing */
Numbers
[edit | edit source]Numbers are a special type of string. They are strings that just contain symbols people would use to represent numbers, such as digits, a negative sign, or a decimal point. Rexx can recognize number strings even if you don't use quotation marks.
/* We can store number strings in variables do math. */ ten = "10" two = '2' say ten + two /* We can write numbers without quotation marks. */ pi = 3.141592654 neg1 = -1 /* We can even use E for scientific notation. */ TwoMillion = 2.0e6
Testing the datatype
[edit | edit source]You can use the DATATYPE function test whether a value is just a character string or specifically a number string. It returns either CHAR or NUM, depending on which is the case.
say DataType("Hello, world!") /* CHAR */ say DataType('1.14193E2') /* NUM */ say DataType("35 meters") /* CHAR */ say DataType(-42) /* NUM */
You can also call the function with a second string, one letter long, to check for even more specific types of string:
- A = Alphanumeric: only letters and digits
- B = Binary: only zeros and ones
- L = Lowercase letters only
- M = Letters only (mixed case)
- N = Number
- U = Uppercase letters only
- W = Whole number
- X = Hexadecimal: digits and letters A-F
The DATATYPE function will return 1 if the first string actually matches that type or 0 if it doesn't.
bits = 101011 say DataType(bits, 'A') /* 1 */ say DataType(bits, 'B') /* 1 */ say DataType(bits, 'L') /* 0 */ say DataType(bits, 'M') /* 0 */ say DataType(bits, 'N') /* 1 */ say DataType(bits, 'U') /* 0 */ say DataType(bits, 'W') /* 1 */ say DataType(bits, 'X') /* 1 */