-
Notifications
You must be signed in to change notification settings - Fork 43
lua 语法
cheyiliu edited this page Jul 23, 2015
·
2 revisions
#!/usr/local/bin/lua
--hello lua
print 'hello lua'
--[[
number test
--]]
print ("===----->number test");
num = 1024 print (num);
num = 3.0 print (num);
num = 3.1416 print (num);
num = 314.16e-2 print (num);
num = 0.31416E1 print (num);
num = 0xff print (num);
num = 0x56 print (num);
--char test
print ("===----->char test");
a = 'alo\n123"'
print (a);
a = "alo\n123\""
print (a);
a = '\97lo\10\04923"'
print (a);
a = [[alo
123"]]
print (a);
--loop
print ("===----->loop test");
print ("sum of 1 to 100");
sum = 0
num = 1
while num <= 100 do
sum = sum + num
num = num + 1
end
print("sum =",sum)
print ("sum of 1 to 100");
sum = 0
for i = 1, 100 do
sum = sum + i
end
print("sum =",sum)
print ("sum of 1 to 100, jishu");
sum = 0
for i = 1, 100, 2 do
sum = sum + i
end
print("sum =",sum)
print ("sum of 1 to 100, oushu");
sum = 0
for i = 100, 1, -2 do
sum = sum + i
end
print("sum =",sum)
--if else
print ("===----->if else test");
age = 30
set = "Male"
if age == 40 and sex =="Male" then
print("男人四十一枝花")
elseif age > 60 and sex ~="Female" then
print("old man without country!")
elseif age < 20 then
io.write("too young, too naive!\n")
else
print("input your age:")
--local age = io.read()
print("Your age is "..age)
end
--function
print ("===----->function test");
function fib(n)
if n < 2 then return 1 end
return fib(n - 2) + fib(n - 1)
end
print(fib(5));
--
function newCounter()
local i = 0
return function() -- anonymous function
i = i + 1
return i
end
end
c1 = newCounter()
print(c1()) --> 1
print(c1()) --> 2
--
function myPower(x)
return function(y) return y^x end
end
power2 = myPower(2)
power3 = myPower(3)
print(power2(4)) --4的2次方
print(power3(5)) --5的3次方
--
function getUserInfo(id)
print(id)
return "haoel", 37, "[email protected]", "http://coolshell.cn"
end
name, age, email, website, bGay = getUserInfo()
print(name);
print(age);
print(email);
print(website);
print(bGay);--nil
--
function foo(x) print('foo(x)'); return x^2 end
print(foo(3));
foo = function(x) print('function(x)'); return x^2 end
print(foo(3));
--table
print ("===----->table test");
haoel = {name="ChenHao", age=37, handsome=true} print(haoel)
for k, v in pairs(haoel) do --遍历
print(k, v)
end
haoel.website="http://coolshell.cn/" print(haoel.website)
local age = haoel.age print(age)
haoel.handsome = false print(haoel.handsome)
haoel.name=nil print(haoel.name)
t = {[20]=100, ['name']="ChenHao", [3.14]="PI"}
print (t[20])
print (t["name"])
print (t[3.14])
arr = {10,20,30,40,50} --等价 arr = {[1]=10, [2]=20, [3]=30, [4]=40, [5]=50}
print(arr)
arr = {"string", 100, "haoel", function() print("coolshell.cn") end}
for i=1, #arr do
print(arr[i])
if i==4 then arr[i]() end
end
for k, v in pairs(arr) do --遍历
print(k, v)
end
--MetaTable 和 MetaMethod???
print ("===----->MetaTable 和 MetaMethod");
fraction_a = {numerator=2, denominator=3}
fraction_b = {numerator=4, denominator=7}
fraction_op={}
function fraction_op.__add(f1, f2)
ret = {}
ret.numerator = f1.numerator * f2.denominator + f2.numerator * f1.denominator
ret.denominator = f1.denominator * f2.denominator
return ret
end
setmetatable(fraction_a, fraction_op)
setmetatable(fraction_b, fraction_op)
fraction_s = fraction_a + fraction_b
print(fraction_s)
Just build something.