位置:首頁 > 高級語言 > Euphoria教學 > Euphoria序列

Euphoria序列

表示序列由大括號{},由逗號分隔的列表中的對象。一個序列可以包含原子和其他序列。例如:

{2, 3, 5, 7, 11, 13, 17, 19}
{1, 2, {3, 3, 3}, 4, {5, {6}}}
{{"Zara", "Ayan"}, 52389, 97.25}
{} -- the 0-element sequence

可以選擇一個單一的元素序列,給予在方括號中的元素個數。元素號從1開始。

例如,如果x包含{5,7.2,0.5,13},則x[2]是7.2。假設X[2]包含{11,22,33},現在如果我們問為x[2],我們得到{11,22,33},如果我們要求的X [2] [3]我們得到原子33。

例子:

#!/home/euphoria-4.0b2/bin/eui

sequence x
x = {1, 2, 3, 4}

for a = 1 to length(x) do
   printf(1, "value of x[%d] = %d\n", {a, x[a]})
end for

這裡的 length() 是一個興奮的內置函數,它返回序列的長度。上麵的例子會產生以下結果:

value of x[1] = 1
value of x[2] = 2
value of x[3] = 3
value of x[4] = 4

字符串

字符串是一個字符序列。這可能是輸入的兩種方法中的一個:

(a) 使用雙引號:

"ABCDEFG"

(b) 使用raw字符串表示:

-- Using back-quotes
`ABCDEFG`

or

-- Using three double-quotes
"""ABCDEFG"""

嘗試下麵的例子來理解這個概念:

#!/home/euphoria-4.0b2/bin/eui

sequence x
x = "ABCD"

for a = 1 to length(x) do
   printf(1, "value of x[%d] = %s\n", {a, x[a]})
end for

這將產生以下結果:

value of x[1] = A
value of x[2] = B
value of x[3] = C
value of x[4] = D

字符串數組

String數組,可以實現使用序列如下:

#!/home/euphoria-4.0b2/bin/eui

sequence x = {"Hello", "World", "Euphoria", "", "Last One"}

for a = 1 to length(x) do
   printf(1, "value of x[%d] = %s\n", {a, x[a]})
end for

這將產生以下結果:

value of x[1] = Hello
value of x[2] = World
value of x[3] = Euphoria
value of x[4] =
value of x[5] = Last One

Euphoria 結構

結構可以采用序列如下:

#!/home/euphoria-4.0b2/bin/eui

sequence employee = {
   {"John","Smith"},
    45000,
    27,
    185.5
   }

printf(1, "First Name = %s, Last Name = %s\n", 
                 {employee[1][1],employee[1][2]} )

這將產生以下結果:

First Name = John, Last Name = Smith

直接序列可以執行以下操作:

一元操作:

當應用到序列的一元運算符實際上是應用序列中的每個元素的結果,得到的序列具有相同的長度。

#!/home/euphoria-4.0b2/bin/eui

sequence x
x = -{1, 2, 3, 4}

for a = 1 to length(x) do
   printf(1, "value of x[%d] = %d\n", {a, x[a]})
end for

這將產生以下結果:

value of x[1] = -1
value of x[2] = -2
value of x[3] = -3
value of x[4] = -4

算術運算:

幾乎所有的算術運算可以執行的序列,如下所示:

#!/home/euphoria-4.0b2/bin/eui

sequence x, y, a, b, c
x = {1, 2, 3}
y = {10, 20, 30}

a = x + y
puts(1, "Value of a = {")
for i = 1 to length(a) do
   printf(1, "%d,", a[i])
end for
puts(1, "}\n")

b = x - y
puts(1, "Value of b = {")
for i = 1 to length(a) do
   printf(1, "%d,", b[i])
end for
puts(1, "}\n")

c = x * 3
puts(1, "Value of c = {")
for i = 1 to length(c) do
   printf(1, "%d,", c[i])
end for
puts(1, "}\n")

這將產生以下結果:

Value of a = {11,22,33,}
Value of b = {-9,-18,-27,}
Value of c = {3,6,9,}

命令行選項:

用戶可以通過命令行選項一個欣快腳本和可以訪問它作為一個序列使用的 command_line() 函數如下:

#!/home/euphoria-4.0b2/bin/eui

sequence x

x = command_line()

printf(1, "Interpeter Name: %s\n", {x[1]} )

printf(1, "Script Name: %s\n", {x[2]} )

printf(1, "First Argument: %s\n", {x[3]})

printf(1, "Second Argument: %s\n", {x[4]})

這裡的 printf() 是一個Euphoria 的內置函數。現在,如果你運行這個腳本如下: 

$eui test.ex "one" "two"

這將產生以下結果:

Interpeter Name: /home/euphoria-4.0b2/bin/eui
Script Name: test.ex
First Argument: one
Second Argument: two