替换字符串"abcdefgh" 中的"abc"为"ddc"
local str ="abcdefgh";
b = string.gsub(str, "%abc", "ddc");
str = b;
print(str);
https://my.oschina.net/workhelu/blog/363949
pairs
1.pairs遍历table中的所有的key-vale 而ipairs会根据key的数值从1开始加1递增遍历对应的table[i]值
a = {[1] = "a1", [2] = "a2", [3] = "a3", [5] = "a4", [6] = "a5",} for key, value in ipairs(a) do print(key, value) end 结果: 1 a1 2 a2 3 a3 a = {[1] = "a1", [2] = "a2", [3] = "a3", [5] = "a4", [6] = "a5",} for key, value in pairs(a) do print(key, value) end 结果: 6 a5 2 a2 3 a3 1 a1 5 a4
function clone( object )
local lookup_table = {}
local function copyObj( object )
if type( object ) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = {}
lookup_table[object] = new_table
for key, value in pairs( object ) do
new_table[copyObj( key )] = copyObj( value )
end
return setmetatable( new_table, getmetatable( object ) )
end
return copyObj( object )
end
local t1 = { "a","b","c"}
-- local t2 = clone(t1);
--t1[1] = "abc";
local t2 = t1;
t1[1] = "abc";
for i=1,#t2 do
print(t2[i]);
end