跳轉到內容

Rexx 程式設計/Rexx 指南/陣列

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

rexx 指令碼語言支援關聯容器。

什麼是陣列?

[編輯 | 編輯原始碼]

陣列是一個包含多個元素的變數,它提供了一種方便的方式,可以將大量值分組在單個變數名下。陣列有點像帶有多個儲存位置的鴿子洞隔間。在 Rexx 中編寫陣列的一種方法是使用所謂的“莖變數”,這些變數使用句點。

/* Let's use a stem variable to store three color names. */
color.1 = "red"
color.2 = "green"
color.3 = "blue"

在上面的程式碼中,“color”是陣列的名稱;但是,有三個顏色名稱,因此每個顏色名稱都與一個數字一起儲存。該數字被稱為索引。例如,第二個顏色名稱(即“green”)在“color.2”莖變數中使用索引 2 儲存。如果我們想儲存第四個顏色名稱,我們只需給它一個第四個索引,例如數字 4。

color.4 = "magenta"

每次使用以前從未使用過的新的索引在陣列中儲存值時,Rexx 會自動為陣列分配更多計算機記憶體,以確保陣列足夠大。我們可以輕鬆新增第五種顏色、第六種顏色、第七種顏色等等。這與其他一些語言不同,這些語言要求您事先說明陣列有多大。

陣列可以儲存多個標量值

[編輯 | 編輯原始碼]

陣列包含稱為元素的一組標量值。儲存陣列的變數稱為陣列變數,它可以儲存任意數量的元素,並且可以是稀疏的或空的。

/* This first array has two elements. */
size.1 = "small"
size.2 = "large"
 
/* The next array has four elements, but the count starts at zero. */
flavor.0 = "vanilla"
flavor.1 = "chocolate"
flavor.2 = "strawberry"
flavor.3 = "banana"

/* We can make an array larger later by just assigning more values. */
flavor.4 = "blueberry"
flavor.5 = "matcha"
size.3 = "supersize"

使用迴圈填充陣列相當普遍,尤其是在使用者需要一次輸入或檢視大量資料時。

/* The user can enter as many names as will fit in memory. */
say "How many names do you want to enter?"
pull quantity
do count = 1 to quantity
 say "Enter a new name please:"
 pull name.count
end

say "Here are all the names you entered:"
do count = 1 to quantity
 say name.count
end

if quantity > 0 then do
 say "The first name you entered was:" name.1
 say "The last name you entered was:" name.quantity
end

陣列可以是多維的

[編輯 | 編輯原始碼]

在 rexx 中,可以使用多維陣列。這意味著我們需要不止一個索引才能在陣列中精確定位一個值。將多維陣列視為由其他陣列組成的陣列的一種方法。多維陣列不是按順序排列的值列表,而是列表的列表。

/* One-dimensional array */
a.1 = "first string"
a.2 = "second string"
a.3 = "third string"

/* Two-dimensional array */
b.1.1 = "first item of first list"
b.1.2 = "second item of first list"
b.2.1 = "first item of second list"
b.2.2 = "second item of second list"

/* Three dimensional array */
c.1.1.1 = "Maybe this represents the first room on the first floor of building 1?"

二維陣列通常用於在記憶體中儲存值表。例如,以下是一個使用二維陣列建立乘法表的指令碼

/* Arrange some products in rows and columns. */
do row = 0 to 12
 do column = 0 to 12
  table.row.column = column * row
 end
end

/* Let's look up a value in the table. */
say "3 times 7 equals" table.3.7

另請參見使用 3 維陣列“A”的示例

  I     = 1           
  J     = 1           
  K     = 1           
  N     = 1           
                      
  DO WHILE I < 6      
    DO WHILE J < 6    
      DO WHILE K < 6  
         A.I.J.K = N  
         K = K + 1    
         N = N + 1    
      END             
      K = 1           
      J = J + 1       
    END               
    J = 1             
    I = I + 1         
  END                 

舉例說明一個元素

  I=5                 
  J=4                 
  K=2                 
  SAY  A.3.4.1  /* pount out one array must have the value 66 in this example */

陣列可以是稀疏的

[編輯 | 編輯原始碼]

在 rexx 中,陣列可以是稀疏的。這意味著並非每個陣列位置都必須具有值或被初始化。陣列中可以有空槽或位置,這些位置不包含資料元素。

/* Judith lives in room 1, and Michael lives in room 2 */
resident.1 = "Judith"
resident.2 = "Michael"

/* Carlos lives in room 5. */
resident.5 = "Carlos"

/* So far rooms 3 and 4 are empty. This is a sparse array. */
華夏公益教科書