跳轉到內容

Rexx 程式設計/Rexx 指南/資料型別

來自華夏公益教科書,開放的書籍,面向開放的世界

原則上,Rexx 只有一種資料型別:字串。字串代表儲存在計算機記憶體中的字母、數字、空格、標點符號和其他型別的文字。在實踐中,Rexx 識別幾種不同的字串形式,並可以在不同的方式使用它們。因此,我們可以說在整個字串型別中存在不同的資料型別。

字串是文字字元的序列。它們可以儲存在變數中,在程式中生成,或者使用單引號或雙引號直接寫入程式碼中。未初始化的變數被解釋為其自身名稱的大寫版本。

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 */

數字是一種特殊的字串。它們是隻包含人們用來表示數字的符號的字串,例如數字、負號或小數點。即使您不使用引號,Rexx 也能識別數字字串。

/* 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

測試資料型別

[編輯 | 編輯原始碼]

您可以使用 DATATYPE 函式來測試一個值是否僅僅是一個字元字串,或者專門是一個數字字串。它將返回 CHAR 或 NUM,取決於具體情況。

say DataType("Hello, world!")   /* CHAR */
say DataType('1.14193E2')       /* NUM  */
say DataType("35 meters")       /* CHAR */
say DataType(-42)               /* NUM  */

您也可以使用一個長度為一個字母的第二個字串來呼叫該函式,以檢查更具體的字串型別。

  • A = 字母數字:只包含字母和數字
  • B = 二進位制:只包含零和一
  • L = 僅小寫字母
  • M = 僅字母(混合大小寫)
  • N = 數字
  • U = 僅大寫字母
  • W = 整數
  • X = 十六進位制:數字和字母 A-F

如果第一個字串實際上與該型別匹配,則 DATATYPE 函式將返回 1;如果它不匹配,則返回 0。

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 */
華夏公益教科書