組み込みが容易なスクリプト言語です。
[vagrant@localhost lua]$ lua -v Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio [vagrant@localhost lua]$ lua hello.lua hello world
luaは多重代入も可能です。
x, y = 10, 15 x, y = y, x print(x) print(y)
文字列ではシングル、ダブルクォーテーションどちらも可能です。
s = "h'e'llo" print(s)
配列:luaは1からカウントします。要素の個数は#です。
a = {23, 234, "hello"}
print(a[2])
print(#a)
条件分岐
score = 75
if score > 60 and score < 80 then
print("ok")
else
print("ng!")
end
ループ
i = 0 while i < 10 do print(i) i = i + 1 end
for文
for i = 0, 9, 2 do print(i) end
構文
a = {12, 24, "hey"}
b = {name = "nari", score= 120}
for i, value in ipairs(a) do
print(i, value)
end
関数
function greet(name)
print("hello, I am "..name)
end
greet("clinton")
可変引数
function sum(...)
local a = {...}
local total = 0
for i = 1, #a do
total = total + a[i]
end
return total
end
print(sum(2, 7, 23141, 131))
便利な命令文
math.max(2, 222, 14) math.ceil(2.3) math.floor(2.3) math.random() math.random(n) -- 1からnまでの整数値
文字列の命令文
s = string.len("google")
s = #"google"
s = string.sub("google", 2, 3)
s = string.find("google", "l")
s = string.gsub("google", "e", "er")
s = string.reverse("google")
テーブルの命令文
a = {2, 25, 42, 1}
table.sort(a)
for i, v in inpairs(a) do
print(v)
end
日付データの命令文
x = os.time() x = os.date() print(x)