当我在 Lua 中运行 table.maxn() 或 table.getn() 时,出现以下错误:
> table.maxn(a)
stdin:1: attempt to call a nil value (field 'maxn')
stack traceback:
stdin:1: in main chunk
[C]: in ?
> table.getn(a)
stdin:1: attempt to call a nil value (field 'getn')
stack traceback:
stdin:1: in main chunk
[C]: in ?
Run Code Online (Sandbox Code Playgroud)
当我尝试探索表对象的内容时,我得到以下结果。就好像库中缺少某些功能一样。
> for k,v in pairs(table) do
>> print (k)
>> end
remove
insert
move
sort
concat
unpack
pack
>
Run Code Online (Sandbox Code Playgroud)
我正在使用 Lua5.3 - 来自下载的 win32 二进制文件 > Lua53.exe
我已确认我没有以任何方式更改/影响表对象。以上结果是重新启动解释器后得到的。
可能是什么问题?
我正在尝试创建一个允许 args 的白名单,以便从 args 表中删除表中不在我的白名单表中的任何提供的 args。
local args = {
"99",
"lollypop",
"tornado",
"catid",
"CATID",
"filter_mediaType",
"one",
"10",
}
local args_whitelist = {
"beforeafter",
"catid",
"childforums",
"display",
"element_id",
"element_type",
"exactname",
"filter_mediaType",
"filter_order",
"filter_order_Dir",
"filter_search",
"filter_tag",
"format",
"id",
"Itemid",
"layout",
"limit",
"limitstart",
"messageid",
"more",
"option",
"order",
"ordering",
"quality",
"query",
"recently",
"recip",
"reply_id",
"return",
"searchdate",
"searchf",
"searchphrase",
"searchuser",
"searchword",
"sortby",
"start",
"task",
"tmpl",
"token",
"view",
"component",
"path",
"extension"
}
--[[
Do something here to eliminate and remove unwanted arguments from table
]] …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个表,并在每次获得表中尚不存在的返回值时添加到该表中。所以基本上我到目前为止所拥有的根本不起作用。我是 LUA 新手,但对一般编程不是很熟悉。
local DB = {}
local DBsize = 0
function test()
local classIndex = select(3, UnitClass("player")) -- This isn't the real function, just a sample
local cifound = False
if classIndex then
if DBsize > 0 then
for y = 1, DBsize do
if DB[y] == classIndex then
cifound = True
end
end
end
if not cifound then
DBsize = DBsize + 1
DB[DBsize] = classIndex
end
end
end
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用另一个函数来打印表的内容:
local x = 0
print(DBsize)
for x = …Run Code Online (Sandbox Code Playgroud) 我们可以做string.split("1,2,3",",")或("1,2,3"):split(",")并得到相同的结果。
但是tab = {} table.insert(tab, "hi")在tab = {} tab:insert("hi")抛出错误时有效
tab = {} tab:insert("hi"):1: 尝试调用一个 nil 值
这似乎不一致,我做错了什么还是有充分的理由为什么在 Lua 中调用方法不同?
谢谢,
我有2个lua表:
OrderTbl = {'Hello', 'Question', 'Answer', 'Bye'}
UnsortedTbl = {'Question', 'Bye, 'Bye', 'Question', 'Hello', 'Something'}
Run Code Online (Sandbox Code Playgroud)
如何按照 OrderTbl 给出的顺序对 UnsortedTbl 进行排序?(OrderTbl中未找到的字段放在结果表的末尾,未排序)
我已经翻译了 Java 的代码示例,它适用于number。这里是:
function first(arr, low, high, x, n)
if high >= low then
-- (low + high)/2
local mid = low + math.floor((high - low) / 2)
if (mid == 1 or x > arr[mid - 1]) and arr[mid] == x then
return mid
end
if x > arr[mid] then return first(arr, (mid + 1), high, x, …Run Code Online (Sandbox Code Playgroud) 例如:
items = {
[753] = {
},
[192] = {
},
[789] = {
},
[791] = {
},
[790] = {
},
[776] = {
},
}
Run Code Online (Sandbox Code Playgroud)
我想删除789及其中的所有数据.我试过了两个:table.remove(items,2); 和table.remove(items,789); (我不确定索引是如何工作的)没有运气.
当我使用luaxml来解析XML字符串时,我对行为感到困惑.Lua doc声明在表变量上调用print():
print(type(t))
print(t)
Run Code Online (Sandbox Code Playgroud)
将产生如下输出:
t2: table
t2: table: 0095CB98
Run Code Online (Sandbox Code Playgroud)
但是,当我使用luaxml时:
require "luaxml"
s = "<a> <first> 1st </first> <second> 2nd </second> </a>"
t = xml.eval(s)
print("t: ", type(t))
print("t: ", t)
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
t: table
t: <a>
<first>1st</first>
<second>2nd</second>
</a>
Run Code Online (Sandbox Code Playgroud)
为什么不print(t)返回看起来像第一个例子的结果?
例如,我有一张桌子
table.insert( t, 1, function()
print ("rock");
end );
Run Code Online (Sandbox Code Playgroud)
有没有办法从这个表中获取函数名称.我知道我可以像键一样存储名称,但如果我想保留数字索引并且我想知道函数名称怎么办?有什么办法吗?谢谢,提前.
我正在尝试添加一个从表目标中随机选择对象的函数.我读到了你可以使用的地方targets[math.random(#targets)],但是当我这样做时,它不仅会重置其中一个目标而不管resetTarget()呼叫,并且它实际上并不会使下一个目标随机.
local targets -- an array of target objects
local bomb = display.newImage("bomb.png")
local asteroid = display.newImage("asteroid.png")
local balloon = display.newImage("balloon.png")
targets = { bomb, asteroid, balloon }
function createTarget()
for i = 1, #targets do
local t = targets[i]
t.x = WIDTH + 50 -- start slightly off screen to the right
t.y = math.random(100, HEIGHT - 100) -- at random altitude
end
end
function resetTarget(obj)
createTarget()
end
function detectHits()
-- Do hit detection for …Run Code Online (Sandbox Code Playgroud) 所以我是lua的新手,我无法弄清楚你如何在数组中的数组中定位元素.
并且是一个与数组相同的表吗?为什么数组和对象具有相同的语法?有区别吗?