lua 的几个常识例子

来源:互联网 发布:2018上海人工智能展会 编辑:程序博客网 时间:2024/04/27 17:00


-- example ---- 02
print("-- example ---- 02");
function pythagorean(a,b)
    local c = a^2 + b^2;
    return math.sqrt(c);
end
print(pythagorean(3, 4));

-- example ---- 03
print("-- example ---- 03");
for i = 1, 5 do
    print("i now is " .. i);
end

-- example ---- 04
print("-- example ---- 04");
for i = 1, 5 do
    print("i now is " .. i);
    if i < 2 then
        print("small");
    elseif i < 4 then
        print("medium");
    else
        print("big");
    end
end

local i = 1;
while i <= 5 do
    print("i now is " .. i);
    if i < 2 then
        print("small");
    elseif i < 4 then
        print("medium");
    else
        print("big");
    end
    i = i + 1;
end

i = 1;
repeat
    print("i now is " .. i);
    if i < 2 then
        print("small");
    elseif i < 4 then
        print("medium");
    else
        print("big");
    end
    i = i + 1;
until i > 5;

-- example ---- 05
print("-- example ---- 05");
-- array
myData = {};
myData[1] = "foo";
myData[2] = 42;
-- hash tables
myData.bar = "bar";
myData["bar"] = "bar";
--print(myData["bar"]);
--print(myData.bar);
-- iterate through the structure
for key, value in pairs(myData) do
     print(key .. "=" .. value);
end
print("--****************************");

a = {};
a["x"] = 10;
b = a;
print(a["x"]);
print(b["x"]);
b["x"] = 23;
print(a["x"]);
print(b["x"]);
a = nil;
b = nil;

-- example ---- 06
print("-- example ---- 06");
myPolygon = {color = "blue", thickness = 2, npoints = 4,
             {x = 0, y = 0},
             {x = -10, y = 0},
             {x = -5, y = 0},
             {x = 0, y = 4}
             };
    -- print the color
print(myPolygon["color"]);
print(myPolygon.color);
    -- print the second point's x
print(myPolygon[2].x);

-- example ---- 07
print("-- example ---- 07");
function funky_print(...)
    for i = 1, arg.n do
        print("funky : " .. arg[i]);
    end
end
funky_print("one", "two");




-- example ---- 08
print("-- example ---- 08");
    -- functioin with tables
    -- parameters

local t={x=10, y=23}
function print_contents(t)
    for k, v in pairs(t) do
        print(tostring(k) .. "=" .. tostring(v));
    end
end

print(t[1]);
print(t[2]);
print_contents(t);
print("**************");
function print_table_test()
    local t_t = {10, 23, x = 10, y = 23};
    for k, v in pairs(t_t) do
        print(k .. "=" .. v);
    end
end
print_table_test();

-- example ---- 09 // 判断数据类型
print("-- example ---- 09");
t_t = {};
print(type(t_t));
t_str = "abcdefghijklmnopqrstuvwxyz";
if type(t_str) == string then -- 如何判断一个数据是某个类型??
    print("t_str is : " .. type(t_str));
end

-- example ---- 10            // 获取子 table 中的元素
print("-- example ---- 10");
network = {
    {name = "grauna",         ip = "210, 26, 30, 34"},
    {name = "arraial",        ip = "210, 26, 30, 23"}
    };
for k, v in pairs(network) do
    print(type(v));
    print(type(k));
    for k1, v1 in pairs(v) do
        print(type(k1));
        print(type(v1));
        print(k1 .. "=" .. v1);
    end
end