我找不到问题的答案:Can you have an array as value in a lua table?
local colors = {"blue" = {0,0,1,1}, "green" = {0,1,0,1}, "red" = {1,0,0,1} , "orange" = {0.5, 0, 0.5, 1}, "black" = {0,0,0,1}, "gold" = {1, 215/255, 0, 1}}
Run Code Online (Sandbox Code Playgroud)
我使用 corona sdk 在这一行收到错误:
'}' 预计位于 '=' 附近
我有包含多个值的表,我想打印每个值。
像是:
“值_1”“值_2”等等。
table = {
{'value_1'},
{'value_2'},
{'value_3'},
{'value_4'},
}
Run Code Online (Sandbox Code Playgroud)
我尝试过for k, v但失败了:
for k, v in pairs(table) do
print(v)
end
Run Code Online (Sandbox Code Playgroud) 我有一个由键/值对组成的表:
mytable[a] = 1,
mytable[b] = 4,
mytable[r] = 7,
mytable[f] = 2,
Run Code Online (Sandbox Code Playgroud)
ETC。
我想按数字对表格进行排序。所以我希望表格为 {(a, 1), (f, 2), (b, 4), (r, 7)} 我尝试使用
table.sort(mytable, function(a, b) return a[2] > b[2] end)
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用...谢谢
当我使用dolua 关键字作为表的键时,会出现以下错误
> table.newKey = { do = 'test' }
stdin:1: unexpected symbol near 'do'
>
Run Code Online (Sandbox Code Playgroud)
我需要用作do密钥。我应该怎么办 ?
[我读过Lua手册,但它没有提供可靠的答案.]
假设我有一个Lua Table,充当索引数组:
local myArray = {};
myArray[1] = "Foo";
myArray[2] = "Bar";
Run Code Online (Sandbox Code Playgroud)
我该如何最好地处理这张桌子?我只是将myArray设置为nil吗?或者我是否必须遍历数组并将每个索引元素设置为nil?
同样地,假设我有一个Lua表,充当字典:
local myDictionary = {};
myDictionary["key1"] = "Foo";
myDictionary["key2"] = "Bar";
Run Code Online (Sandbox Code Playgroud)
我可以将'myDictionary'设置为nil,还是必须迭代?
最后,我该怎么做,内存管理明智,我有嵌套表的地方?例如
local myNestedCollection = {};
myNestedCollection[1] = {1, 2, 3};
myNestedCollection[2] = {4, 5, 6};
Run Code Online (Sandbox Code Playgroud)
我是否需要遍历每个子表,将它们设置为nil?谢谢你的帮助.
我试图在表中存储不同的函数,但不知何故它不会像我想象的那样工作.这是我的'代码'
fn_table = { aFun1=print, aFun2=self:getSpeedLevel, aFun3=.... }
Run Code Online (Sandbox Code Playgroud)
现在的问题是我可以使用内置函数来完成这个print,assert等等,但它不能与我得到的其他函数一起使用.
我收到错误:"...函数参数预计在'}'附近
是否也可以存储这些功能?
我是lua的新手.我需要将以下字符串转换为lua表.我怎么能这样做?
str = "{a=1, b=2, c={d=3,e=4} }"
Run Code Online (Sandbox Code Playgroud)
我想将此字符串转换为lua表,以便我可以像这样访问它:
print(str['a']) -- Output : 1
print(str['c']['d']) -- Output : 3
Run Code Online (Sandbox Code Playgroud) 通过使用Python,我们可以将字符串重新定义为字典.
示例:
var = "testing"
var = {'body': var}
print var['body']
'testing'
Run Code Online (Sandbox Code Playgroud)
有了Lua,我想做同样的事情.我想将字符串对象转换为表格.
我的尝试:
> var = "testing"
> var = {'body', var}
> print(var)
table: 0x12b2930
> for i,j in pairs(var) do
>> print(i)
>> print(j)
>> end
1
body
2
testing
>
Run Code Online (Sandbox Code Playgroud)
通过上面的例子,我可以获取测试字符串
> print(var[2])
testing
>
Run Code Online (Sandbox Code Playgroud)
以上代码不符合我的要求.我想'testing'用"body"key 存储值.
我想获取如下:
print(var['body'])
'testing'
Run Code Online (Sandbox Code Playgroud)
请帮帮我
我试图在文本文件中的一行文本上匹配三个数据并将它们存储在表元素中.每行看起来像这样:
0.277719 0.474610 This
0.474610 0.721241 is
0.721241 1.063209 test
Run Code Online (Sandbox Code Playgroud)
我有一个本地表来保存文本行,我正在尝试分配数据如下.
local data = {}
local file = io.open( "audio/audio.txt", "r" )
local i = 1
for line in file:lines() do
data[i] = line
data[i].start, data[i].out, data[i].name = string.match( line, '(%S+)%s*(%S+)%s*(%S+)' )
i = i + 1
end
Run Code Online (Sandbox Code Playgroud)
该data[i] = line部分工作正常.下一行没有.我得到的就是以下错误data[i].start, data[i].out, data[i].name = string.match( line, '(%S+)%s*(%S+)%s*(%S+)' ):
attempt to index field '?' (a string value)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我在lua中有这个表:
local values={"a", "b", "c"}
Run Code Online (Sandbox Code Playgroud)
如果变量等于表条目中的一个,有没有办法返回表的索引?说
local onevalue = "a"
Run Code Online (Sandbox Code Playgroud)
如何在不迭代所有值的情况下获取表中"a"或onevalue的索引?