在为C++类创建Lua绑定时,我应该返回表还是userdata对象?
有谁知道每种方法的优缺点?
我无法获得表项索引.我需要它从表中删除一个项目.
我用来table.insert向表添加条目.
另一个问题:为什么Lua没有"重载"函数table.remove所以可以通过关联索引删除项目?
有没有办法使用table.concat的arg 2值来表示当前的表索引?
例如:
t = {}
t[1] = "a"
t[2] = "b"
t[3] = "c"
X = table.concat(t,"\n")
Run Code Online (Sandbox Code Playgroud)
表concat(X)的所需输出:
"1 a\n2 b\n3 c\n"
Run Code Online (Sandbox Code Playgroud) 来自PiL(第1版和第2版):
虽然我们可以使用insert和remove(从表库中)轻松实现队列,但对于大型结构,此实现可能太慢.更有效的实现使用两个索引,一个用于第一个,另一个用于最后一个元素......
这是因为重新编制索引吗?或者还有另一个原因导致大型结构的效率低下?
到目前为止我有这个
mk= {}
mk = conn:query({ live=true, sql="select * from OrderReports where OrderId= '"..T.OrderId.."'"})
for a=1, # mk do
end
Run Code Online (Sandbox Code Playgroud)
我希望将不同索引中的字符串mk连接起来并存储在变量中,比如说lk,身体应该是什么?
我有以下unpack()功能:
function unpack(t, i)
i = i or 1
if t[i] then
return t[i], unpack(t, i + 1)
end
end
Run Code Online (Sandbox Code Playgroud)
我现在在以下测试代码中使用它:
t = {"one", "two", "three"}
print (unpack(t))
print (type(unpack(t)))
print (string.find(unpack(t), "one"))
print (string.find(unpack(t), "two"))
Run Code Online (Sandbox Code Playgroud)
哪个输出:
one two three
string
1 3
nil
Run Code Online (Sandbox Code Playgroud)
令我困惑的是最后一行,为什么会有结果nil呢?
我有问题; 我必须在表格中查看我的程序中的一个字段.
if(launchArgs.androidIntent.extras.notification.custom.test_field ~= nil)then...
Run Code Online (Sandbox Code Playgroud)
当这个索引存在时一切正常,但是当它不存在时,我收到一个错误:
Attempt to index field 'notification' (a nil value).
Run Code Online (Sandbox Code Playgroud)
这是可以理解的.如何检查该索引是否存在?
我写了一个例子.
function readOnly(t)
local newTable = {}
local metaTable = {}
metaTable.__index = t
metaTable.__newindex = function(tbl, key, value) error("Data cannot be changed!") end
setmetatable(newTable, metaTable)
return newTable
end
local tbl = {
sex = {
male = 1,
female = 1,
},
identity = {
police = 1,
student = 2,
doctor = {
physician = 1,
oculist = 2,
}
}
}
local hold = readOnly(tbl)
print(hold.sex)
hold.sex = 2 --error
Run Code Online (Sandbox Code Playgroud)
这意味着我可以访问表"tbl"的字段,但同时,我无法更改与该字段相关的值.
现在,问题是我想让所有嵌套表拥有这个只读属性.如何改进"readOnly"方法?
所以我有一个变量,我们称之为'ID'.我需要相对于固定数量的值检查此值.当然,ID只能匹配其中一个值,因此不会出现停止第一个匹配值的问题,因为其他值都不匹配.变量也有可能与任何给定值都不匹配.那么我的问题是,资源效率最高的方法是什么?我可以想到解决这个问题的两种简单方法.由于我知道编程时的值,我可以使用'或'设置条件,只检查每个值,如下所示:
if (ID == "1" or ID == "16" or ID == "58") then
--do something--
end
Run Code Online (Sandbox Code Playgroud)
这样做的问题在于它写起来非常冗长乏味.另一个选项涉及foreach循环,我预先定义一个表.
values = {"1", "16", "58"}
for _, value in ipairs(values) do
if(ID == value) then
return true
end
end
Run Code Online (Sandbox Code Playgroud)
这样做的好处是它可以重复使用,因为我需要使用不同的值集进行至少10次精确检查,缺点是我怀疑需要更多资源.
任何帮助将不胜感激.
我想在我的Lua程序中有一个只读表。如果密钥被删除或密钥与新值相关联,则必须引发错误。
function readonly(table)
local meta = { } -- metatable for proxy
local proxy = { } -- this table is always empty
meta.__index = table -- refer to table for lookups
meta.__newindex = function(t, key, value)
error("You cannot make any changes to this table!")
end
setmetatable(proxy, meta)
return proxy -- user will use proxy instead
end
Run Code Online (Sandbox Code Playgroud)
效果很好。
t = { }
t["Apple"] = "Red"
t[true] = "True!"
t[51] = 29
for k,v in pairs(t) do
print(v)
end
t = …Run Code Online (Sandbox Code Playgroud)