这是一个Lua新手.我可以将函数引用存储为Lua表中的键吗?与此类似的东西:
local fn = function() print('hello') end
local my_table = {}
my_table[fn] = 123
Run Code Online (Sandbox Code Playgroud)
这似乎工作正常,但我不知道我是否可以依赖函数引用的唯一性.Lua可以在超出范围时重用函数引用吗?这会产生任何问题,还是由于某种原因被认为是不好的做法?
有没有办法可以将层次结构字符串转换为表格形式?
假设输入是 A.B.C.D
输出应该是一个遍历输入的表:
A = {}
A.B = {}
A.B.C = {}
A.B.C.D = {}
谢谢.
作为初学者,我在Lua中遇到了一个简单的问题:
a = function()
print("hello")
end
b = {125, 116, a()}
print(b[1])
Run Code Online (Sandbox Code Playgroud)
它应该只打印125,但也打印hello.即使未选择表值.
我有一个全局表,我希望在两个不同的Lua 状态之间保持同步.从我所阅读和理解的,唯一的方法似乎是,在我的C后端,在状态之间执行表的深层复制(如果表已被修改).有没有更好的办法 ?另外,我看到一些Lua片段用于表格深层复制,但不是在C中,是否有任何库[在C]中执行此操作?
PS我不是在找lua_thread基础解决方案(我已经在使用它了)
PPS Lua Lanes似乎很接近,但IMO似乎太多了,因为我只想同步1张桌子!
我试图从Lua中的表中减去表,因此返回表将是从t2减去t1.
这似乎有效,但有更有效的方法吗?
function array_sub(t1, t2)
-- Substract Arrays from Array
-- Usage: nretable = array_sub(T1, T2) -- removes T1 from T2
table.sort( t1 )
for i = 1, #t2 do
if (t2[i] ~= nil) then
for j = 1, #t1 do
if (t2[i] == t1 [j]) then
table.remove (t2, i)
end
end
end
end
return t2
end
local remove ={1,2,3}
local full = {}; for i = 1, 10 do full[i] = i end
local test ={}
local test = …Run Code Online (Sandbox Code Playgroud) 我对访问Lua表中的数据有疑问。
说,有一个很大的Lua表,如下所示:
tbl = {
{
blockIdx = 5,
key1 = "val1",
key2 = "val2",
...
},
{
blockIdx = 30,
key1 = "val11",
key2 = "val12",
...
},
{
blockIdx = 83,
key1 = "val21",
key2 = "val22",
...
},
...
}
Run Code Online (Sandbox Code Playgroud)
现在,我想找到一个块,blockIdx例如38。
因此,通常情况下,我想使用for该块:
for k,v in pairs(tbl) do
if v.blockIdx == 38 then
blahFunction(v)
end
end
Run Code Online (Sandbox Code Playgroud)
但是我认为这不是一个好主意,特别是对于大桌子。
所以我对表做了一些修改:
tbl = {
[5] = {
key1 = "val1",
key2 = "val2",
...
}, …Run Code Online (Sandbox Code Playgroud) 如何#在Lua中更改表的长度operator(),手册建议__len在metatable中分配函数,然后将metatable分配给我想要覆盖的表,但是这不能按预期工作?我没有选择在C端覆盖它.
turtles = {1,2,3}
setmetatable(turtles, {__len = function(mytable) return 5 end})
print(#turtles)
--returns 3, should return 5
Run Code Online (Sandbox Code Playgroud) 在 Lua 中,我知道有
table.remove(array, index)
Run Code Online (Sandbox Code Playgroud)
有没有一种快速的方法可以从数组中删除和返回 X 元素(而不仅仅是重复调用 table.remove)?
下面是我需要从C读取的lua表:
listen = {
{ port = 1234, address = "192.168.1.1", userdata = "liunx" },
{ port = 1235, address = "192.168.1.2", userdata = "liunx1" },
{ port = 1236, address = "192.168.1.3", userdata = "liunx2" }
}
Run Code Online (Sandbox Code Playgroud)
下面是c代码:
#include <lua.h> /* Always include this when calling Lua */
#include <lauxlib.h> /* Always include this when calling Lua */
#include <lualib.h> /* Prototype for luaL_openlibs(), */
/* always include this when calling Lua */
#include <stdlib.h> /* For function exit() …Run Code Online (Sandbox Code Playgroud) 我想创建一个具有特定名称作为键和特定函数作为值的表.键名表示用户输入的命令,如果存在该名称的键,则程序应执行存储在该键值中的代码.
因此,例如,我们在键值中创建一个包含键和函数的表:
local t = {
["exit"] = quitGame,
...,
...
}
Run Code Online (Sandbox Code Playgroud)
我们还有一个功能,例如:
function quitGame()
print("bye bye")
os.exit()
end
Run Code Online (Sandbox Code Playgroud)
所以现在我们做:
userInput = io.read()
for i,v in pairs(t) do
if userInput == i then
--now here, how do I actually run the code that is stored in that key value (v)?
end
end
Run Code Online (Sandbox Code Playgroud)
我希望你明白我在做什么.