附件A程式碼
if範例
if a <= 0 then a = a + 1 end
if-else範例
if a < 0 then a = a + 1 elseif a > 0 then a = a - 1 else a = 0
while迴圈範例
a = {1,2,3,4,5} -- array index start at 1 i = 1 while a[i] do print(a[i]) i = i + 1 end
repeat迴圈範例
a = 0 repeat a = a + 1 print (a) until a > 10
for迴圈範例
for i=1, 10, 1 do print(i) end
for each迴圈範例
colors={"red", "green", "blue"} for i, v in ipairs(colors) do print(i,v) end
break範例
a = 10 -- while loop execution -- while( a < 20 ) do print("value of a:", a) a=a+1 if (a > 15) then break end end
function範例
function square(x) local sqr = 0 sqr = x * x return sqr end square = function (x) local sqr = 0 sqr = x * x return sqr end print(square.(2)) --> 4 result1 = square "2" print (result1) --> 4 result2 = square (3) print (result2)) --> 9
Closure
function newCounter() local i = 0 return function() i = i + 1 return i end end c1 = newCounter() print(c1()) --> 1 print(c1()) --> 2 c2 = newCounter () print(c2()) --> 1 print(c1()) --> 3 print(c2()) --> 2
String範例
string1 = "TTU" print("\"String 1 is \", string1) string2 = 'SIRD' print("String 2 is", string2) string3 = [["TTUSIRD"]] print("String 3 is", string3)
字串大小寫轉換範例
string1 = "Lua" print(string.upper(string1)) print(string.lower(string1))
字串取代範例
string = "Lua Tutorial" newstring = string.gsub(string,"Tutorial","Language") print("The new string is", newstring)
字串搜尋和反轉範例
string = "Lua Tutorial" print(string.find(string,"Tutorial")) print(string.reverse(string))
格式化字串範例
date = 2; month = 1; year = 2014 print(string.format("Date: %02d/%02d/%03d", date, month, year))
字串的字元和位元轉換範例
print(string.byte("Lua")) print(string.byte("Lua", 3)) print(string.byte("Lua", -1)) print(string.char(97))
字串的長度、連結字串、重複字串範例
string1="Lua" string2="Tutorial" print("Concat:", string1..string2) print("Len:", string.len(string1)) rString = string.rep (string1,3) print(rString)
Array陣列範例
array = {"Lua", "Tutorial"} for i= 0, 2 do print (arrayi) end
Array陣列範例2
array = {} for i= -2, 2 do array[i] = i *2 end for i = -2, 2 do print(array[i]) end
多維陣列範例
-- Initializing the array array = {} for i=1,3 do array[i] = {} for j=1,3 do array[i][j] = i*j end end -- Accessing the array for i=1,3 do for j=1,3 do print(array[i][j]) end end
表格範例
-- 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)
表格內容連結範例
fruits = {"banana", "orange", "apple"} -- returns concatenated string of table print("Concat string ",table.concat(fruits)) --concatenate with a character print("Concat string ",table.concat(fruits,", ")) --concatenate fruits based on index print("Concat string ",table.concat(fruits,", ", 2,3))
插入和移除表格內容以及計算表格內容個數範例
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 max 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)
表格內容排序範例
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