Lua-tables

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

Lua-テーブル

前書き

テーブルはLuaで使用できる唯一のデータ構造であり、配列や辞書などのさまざまなタイプの作成に役立ちます。 Luaは連想配列を使用し、数値だけでなく、nil以外の文字列でもインデックスを作成できます。 テーブルのサイズは固定されておらず、必要に応じて拡大できます。

Luaは、パッケージの表現を含むすべての表現でテーブルを使用します。 メソッドstring.formatにアクセスすると、stringパッケージで利用可能なformat関数にアクセスしていることを意味します。

表現と使用

テーブルはオブジェクトと呼ばれ、値でも変数でもありません。 Luaは、コンストラクタ式\ {}を使用して空のテーブルを作成します。 テーブルの参照を保持する変数とテーブル自体の間に固定された関係はないことが知られています。

--sample table initialization
mytable = {}

--simple table value assignment
mytable[1]= "Lua"

--removing reference
mytable = nil

-- lua garbage collection will take care of releasing memory

要素のセットを持つテーブル a があり、それを b に割り当てると、 ab の両方が同じメモリを参照します。 bに個別に割り当てられるメモリはありません。 aがnilに設定されている場合、テーブルは引き続きbにアクセスできます。 テーブルへの参照がない場合、Luaのガベージコレクションはクリーンアッププロセスを処理し、これらの参照されていないメモリを再利用できるようにします。

上記のテーブルの機能を説明するための例を以下に示します。

-- Simple empty table
mytable = {}
print("Type of mytable is ",type(mytable))

mytable[1]= "Lua"
mytable["wow"] = "Tutorial"

print("mytable Element at index 1 is ", mytable[1])
print("mytable Element at index wow is ", mytable["wow"])

-- alternatetable and mytable refers to same table
alternatetable = mytable

print("alternatetable Element at index 1 is ", alternatetable[1])
print("alternatetable Element at index wow is ", alternatetable["wow"])

alternatetable["wow"] = "I changed it"

print("mytable Element at index wow is ", mytable["wow"])

-- only variable released and and not table
alternatetable = nil
print("alternatetable is ", alternatetable)

-- mytable is still accessible
print("mytable Element at index wow is ", mytable["wow"])

mytable = nil
print("mytable is ", mytable)

上記のプログラムを実行すると、次の出力が得られます-

Type of mytable is  table
mytable Element at index 1 is   Lua
mytable Element at index wow is     Tutorial
alternatetable Element at index 1 is    Lua
alternatetable Element at index wow is  Tutorial
mytable Element at index wow is     I changed it
alternatetable is   nil
mytable Element at index wow is     I changed it
mytable is  nil

テーブル操作

テーブル操作用の組み込み関数があり、それらは次の表にリストされています。

Sr.No. Method & Purpose
1

table.concat (table [, sep [, i [, j]]])

指定されたパラメーターに基づいてテーブル内の文字列を連結します。 詳細については例を参照してください。

2

table.insert (table, [pos,] value)

テーブルの指定された位置に値を挿入します。

3

table.maxn (table)

最大の数値インデックスを返します。

4

table.remove (table [, pos])

テーブルから値を削除します。

5

table.sort (table [, comp])

オプションのコンパレータ引数に基づいてテーブルをソートします。

上記の関数のサンプルを見てみましょう。

テーブル連結

以下に示すように、concat関数を使用して2つのテーブルを連結できます-

fruits = {"banana","orange","apple"}

-- returns concatenated string of table
print("Concatenated string ",table.concat(fruits))

--concatenate with a character
print("Concatenated string ",table.concat(fruits,", "))

--concatenate fruits based on index
print("Concatenated string ",table.concat(fruits,", ", 2,3))

上記のプログラムを実行すると、次の出力が得られます-

Concatenated string     bananaorangeapple
Concatenated string     banana, orange, apple
Concatenated string     orange, apple

挿入と削除

テーブルへのアイテムの挿入と削除は、テーブル操作で最も一般的です。 以下で説明します。

fruits = {"banana","orange","apple"}

-- insert a fruit at the end
table.insert(fruits,"mango")
print("Fruit at index 4 is ",fruits[4])

--insert fruit at index 2
table.insert(fruits,2,"grapes")
print("Fruit at index 2 is ",fruits[2])

print("The maximum elements in table is",table.maxn(fruits))

print("The last element is",fruits[5])

table.remove(fruits)
print("The previous last element is",fruits[5])

上記のプログラムを実行すると、次の出力が得られます-

Fruit at index 4 is     mango
Fruit at index 2 is     grapes
The maximum elements in table is    5
The last element is mango
The previous last element is    nil

テーブルの並べ替え

多くの場合、特定の順序でテーブルをソートする必要があります。 ソート関数は、テーブル内の要素をアルファベット順にソートします。 このサンプルを以下に示します。

fruits = {"banana","orange","apple","grapes"}

for k,v in ipairs(fruits) do
   print(k,v)
end

table.sort(fruits)
print("sorted table")

for k,v in ipairs(fruits) do
   print(k,v)
end

上記のプログラムを実行すると、次の出力が得られます-

1   banana
2   orange
3   apple
4   grapes
sorted table
1   apple
2   banana
3   grapes
4   orange