Euphoria-sequences

提供:Dev Guides
移動先:案内検索

幸福感-シーケンス

シーケンスは、コンマで区切られた中括弧で囲まれたオブジェクトのリスト\ {}で表されます。 シーケンスには、アトムと他のシーケンスの両方を含めることができます。 たとえば-

{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、9、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

文字列

文字列は、単なる*シーケンス*の文字です。 それは2つの方法のいずれかで入力することができます-

(a)二重引用符の使用-

"ABCDEFG"
  • (b)生の文字列表記法を使用する- *
-- 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

文字列配列

文字列の配列は、次のようにシーケンスを使用して実装することができます-

#!/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

陶酔構造

構造は、次のようにシーケンスを使用して実装できます-

#!/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,}

コマンドラインオプション

ユーザーはコマンドラインオプションをEuphoriaスクリプトに渡すことができ、次のように* 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