lua小试牛刀

打印

print("hello world!")

函数调用

function max(num1, num2)
        if(num1 > num2) then
                result = num1;
        else
                result = num2;
        end

        return result;
end

print(max(324,569))

返回多个参数,字符串连接

function ret2(flag)
	return "OXOOO"..flag, flag.."000";
end

pre,next = ret2("7E");
print(pre,next);

for循环

for i=1, 9 do
	for j = 1, i do
		io.write(i.."X"..j.."="..i*j.." ");
	end
	print()
end

if条件

if conn.. then
    ok
else 
    nok
end

数组

array = {"Jey","Tom","Nick"};
for i=1, 3 do
	print(array[i])
end

Jey
Tom
Nick

导入库

local lfs = require"lfs"
local arr = lfs.attributes("D:\source\project\MODEL\xyg\model_stat.xml")

table输出

function print_r ( t )  
    local print_r_cache={}
    local function sub_print_r(t,indent)
        if (print_r_cache[tostring(t)]) then
            print(indent.."*"..tostring(t))
        else
            print_r_cache[tostring(t)]=true
            if (type(t)=="table") then
                for pos,val in pairs(t) do
                    if (type(val)=="table") then
                        print(indent.."["..pos.."] => "..tostring(t).." {")
                        sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
                        print(indent..string.rep(" ",string.len(pos)+6).."}")
                    elseif (type(val)=="string") then
                        print(indent.."["..pos..'] => "'..val..'"')
                    else
                        print(indent.."["..pos.."] => "..tostring(val))
                    end
                end
            else
                print(indent..tostring(t))
            end
        end
    end
    if (type(t)=="table") then
        print(tostring(t).." {")
        sub_print_r(t,"  ")
        print("}")
    else
        sub_print_r(t,"  ")
    end
    print()
end
原文地址:https://www.cnblogs.com/xiongyungang/p/10755847.html