为什么我不能使用table.sort对具有关联索引的表进行排序?
我在Lua中有一个多维表,但我似乎无法创建它以便能够在Lua中使用?
表
items ::= {
{["category"]="tools", ["name"]="hammer", ["price"]=10, ["quantity"]=5 },
{["category"]="tools", ["name"]="saw", ["price"]=15, ["quantity"]=4 },
{["category"]="tools", ["name"]="screwdriver", ["price"]=4, ["quantity"]=12 },
{["category"]="tools", ["name"]="measuring tape", ["price"]=9, ["quantity"]=3 },
{["category"]="tools", ["name"]="pliers", ["price"]=10, ["quantity"]=5 },
{["category"]="tools", ["name"]="wrench", ["price"]=10, ["quantity"]=5 },
{["category"]="fasteners", ["name"]="nails", ["price"]=.1, ["quantity"]=1500 },
{["category"]="fasteners", ["name"]="screws", ["price"]=.2, ["quantity"]=1200 },
{["category"]="fasteners", ["name"]="staples", ["price"]=.05, ["quantity"]=2000 },
}
Run Code Online (Sandbox Code Playgroud)
错误:
'<name>' expect near ':'
我是一个PHP人,所以我不知道如何解决这个问题.我知道我在PHP中如何做到这一点,但我不知道Lua对这个问题有什么限制.
T = {
clocktable = {},
beancabinet = {},
--...etc
}
T.clocktable[674] = 1
T.clocktable[660] = 1
--...etc
Run Code Online (Sandbox Code Playgroud)
问:如何通过"T"循环以快速知道时钟表键包括扩展键"674"和"660",只知道"时钟表"?
注意:请注意开销,因为"T"表将非常加载数据,这是在性能环境中.
我不能让它工作:
tbl = {
[1] = { ['etc2'] = 14477 },
[2] = { ['etc1'] = 1337 },
[3] = { ['etc3'] = 1336 },
[4] = { ['etc4'] = 1335 }
}
for i = 1, #tbl do
table.sort(tbl, function(a, b) return a[i] > b[i] end)
print(tbl[i] .. '==' .. #tbl)
end
Run Code Online (Sandbox Code Playgroud)
获取此错误:尝试比较两个零值
这是lua中表值排序的后续
在Lua中,有没有办法分割这个字符串:
etc3=1336,etc2=14477,etc4=1335,etc1=1337
Run Code Online (Sandbox Code Playgroud)
进这张桌子?
tbl = {
{ 'etc3', 1336 },
{ 'etc2', 14477 },
{ 'etc4', 1335 },
{ 'etc1', 1337 },
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏.
-- original table
t = {
Steve = 20,
Mary = 32,
Tim = 15
}
--second table to help sort t{}
a = {}
for n in pairs(t) do
a[#a + 1] = n -- I do not completely understand this line.
-- I know [#a + 1] is the iterator, but
-- not a[#a + 1] and why equal that to "n"?
end
table.sort(a)
for _, n in ipairs(a) do -- I do not understand the point of …Run Code Online (Sandbox Code Playgroud) 我试图in pairs在函数内部使用但它不起作用,它只打印第一行(键).这是我的代码:
set = {1,2,3,4};
unset = {5,6,7,8};
function listen(ftype)
if (ftype == [[~]]) then
for num,str in pairs(set) do
return str;
end
end
if (ftype == [[$]]) then
for num,str in pairs(unset) do
return str;
end
end
end
print(listen([[~]])..[[ =:= ]]..listen([[$]]));
Run Code Online (Sandbox Code Playgroud)
如果我做这样的事情..
for num,str in pairs(unset) do
print(str);
end
Run Code Online (Sandbox Code Playgroud)
它就像一个魅力.这正是我想要的,但在功能中.
我有一个table(TableA),其中有另一个表(TableB).我想TableA从内部检索一个位于其中的变量TableB.我该怎么做呢?我试过了:
tableA = {
testA = 5;
tableB = {
testB = tableA.testA + 1;
};
}
print(tableA.tableB.testB)
Run Code Online (Sandbox Code Playgroud)
但是从tableB那些tableA不存在的状态中获取错误.
我正在尝试初始化并打印一个表.它只是不工作.知道这个代码有什么问题吗?
--!/usr/bin/env lua
local retv = {}
retv["test"] = 1000
for k,v in ipairs(retv) do
print (k,v)
end
Run Code Online (Sandbox Code Playgroud)
它什么都不打印.我确信我遗漏了一些非常基本的东西,但我无法弄清楚这一点.
是否可以在Lua中不使用元表打印表格?
在Roberto的" Lua编程 " 一书中,他提到"函数print始终要求tostring格式化其输出".但是,如果我tostring在我的表中覆盖,那么我得到以下结果:
> a = {}
> a.tostring = function() return "Lua is cool" end
> print(a)
table: 0x24038c0
Run Code Online (Sandbox Code Playgroud)