Lua学习笔记

LUA学习笔记

注释

单行注释 --
多行注释 --[[
        --]]

Lua数据类型

nil
boolean     true/false
number      浮点
string
function    C或Lua函数
userdata    C数据结构
thread
table       关联数组
tab1 = { key1 = "val1", key2 = "val2", "val3" }
for k, v in pairs(tab1) do
    print(k .. " - " .. v)
end
 
tab1.key1 = nil
for k, v in pairs(tab1) do
    print(k .. " - " .. v)
end

字符串

'Lua'
"Lua"
[[Lua]]

变量

三种类型:全局变量,局部变量,表中的域

a = 5       全局变量
local b =5  局部变量

运算

运算符

算数: + - * / % ^
关系: == ~= > < >= <=
逻辑: and or not
字符串: # ..
> print("2" + 6)
8.0
> print("2" + "6")
8.0
> print("2 + 6")
2 + 6
> print("-2e2" * "6")
-1200.0
> print("a" .. 'b')
ab
> print(157 .. 428)
157428
print(#"www.runoob.com")
14

a, b = 10, 2*x       <-->       a=10; b=2*x

> site = {}
> site["key"] = "www.runoob.com"
> print(site["key"])
www.runoob.com
> print(site.key)
www.runoob.com

函数

普通函数

function factorial1(n)
    if n == 0 then
        return 1
    else
        return n * factorial1(n - 1)
    end
end
print(factorial1(5))
factorial2 = factorial1
print(factorial2(5))

匿名函数

function testFun(tab,fun)
        for k ,v in pairs(tab) do
                print(fun(k,v));
        end
end


tab={key1="val1",key2="val2"};
testFun(tab,
function(key,val)--匿名函数
        return key.."="..val;
end
);

可变参数

function average(...)
   result = 0
   local arg={...}    --> arg 为一个表,局部变量
   for i,v in ipairs(arg) do
      result = result + v
   end
   print("总共传入 " .. #arg .. " 个数")
   return result/#arg
end

print("平均值为",average(10,5,3,4,5,6))

function average(...)
   result = 0
   local arg={...}
   for i,v in ipairs(arg) do
      result = result + v
   end
   print("总共传入 " .. select("#",...) .. " 个数")
   return result/select("#",...)
end

print("平均值为",average(10,5,3,4,5,6))

多返回值

function maximum (a)
    local mi = 1             -- 最大值索引
    local m = a[mi]          -- 最大值
    for i,val in ipairs(a) do
       if val > m then
           mi = i
           m = val
       end
    end
    return m, mi
end

print(maximum({8,10,23,12,5}))

循环

a=10
while( a < 20 )
do
   print("a 的值为:", a)
   a = a+1
end

for i=10,1,-1 do
    print(i)
end

for i=1, (function (x)  
        return x*2  
    end)(5) do print(i)  
end

days = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}  
for i,v in ipairs(days) do  print(v) end   

a = 10
repeat
   print("a的值为:", a)
   a = a + 1
until( a > 15 )

分支

if(0)
then
    print("0 为 true")
end

字符串

string.upper(argument)
string.lower(argument)
string.gsub(mainString,findString,replaceString,num)
string.find (str, substr, [init, [end]])
string.reverse(arg)
string.format(...)
string.char(arg)
string.byte(arg[,int])
string.len(arg)
string.rep(string, n)
string.gmatch(str, pattern)
    string.gmatch("Hello Lua user", "%a+")
string.match(str, pattern, init)
    string.match("I have 2 questions for you.", "%d+ %a+")

数组

array = {"Lua", "Tutorial"}

for i= 0, #array do
   print(array[i])
end

-- 多维数组
array = {}
for i=1,3 do
   array[i] = {}
      for j=1,3 do
         array[i][j] = i*j
      end
end

迭代器

for k, v in pairs(t) do
    print(k, v)
end

array = {"Google", "Runoob"}

function elementIterator (collection)
   local index = 0
   local count = #collection
   -- 闭包函数
   return function ()
      index = index + 1
      if index <= count
      then
         --  返回迭代器的当前元素
         return collection[index]
      end
   end
end

table

table.concat (table [, sep [, start [, end]]])
table.insert (table, [pos,] value)
table.maxn (table)
table.remove (table [, pos])
table.sort (table [, comp])

模块

require("模块名")
require "模块名"
local path = "/usr/local/lua/lib/libluasocket.so"
local f = loadlib(path, "luaopen_socket")

你可能感兴趣的:(Lua学习笔记)