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
- lua 的几个常识例子
- [LUA]几个简单的luajava使用例子
- Lua时间转化的几个小例子
- Cocos2d-精灵的几个常识
- 关于计算机硬件的几个常识问题
- 你必须知道的几个救命常识
- HMC管理的几个小常识
- 做网站需注意的几个常识
- HMC管理的几个小常识
- 【学习笔记】java的几个概念常识
- 【分析必须知道的几个电脑硬件常识】
- C语言的几个小常识
- HMC管理的几个小常识
- OracleHelper的几个例子.
- Papervision3D的几个例子
- set_union的几个例子
- QFont的几个例子
- set_union的几个例子
- 在Ubuntu9.04上编译koffice2.1Beta3
- 注册了好长时间!!潜水了好长时间!!从今天开始,要好好学习天天向上!!!!!
- Ubuntu、云计算与IBM Vs Win7
- IBM云计算的生命周期
- 报到
- lua 的几个常识例子
- hibernate学习(一)
- linux系统调用列表
- VC获取其他进程ListCtrl内容
- Ext.grid.ColumnModel
- Core Image 学习笔记 - 为照片加边框和EXIF信息
- 文件解析(1)
- 解析日志文件入库的代码
- 关于数据驱动和关键字驱动的理解整理