Indexing Lua索引仅适用于哪里?
让我先显示我的代码行:Indexing Lua索引仅适用于哪里?,indexing,lua,Indexing,Lua,让我先显示我的代码行: local names = {} str = "Hello World" for count = 0, #str do names[count] = string.sub(str, count, count) names[count].id = count end 顺便说一句,这只是一个例子,但我的概念在那里。无论如何,它总是返回一个错误,即尝试索引字段“?”(一个nil值)。这个错误意味着什么?我试着
local names = {}
str = "Hello World"
for count = 0, #str do
names[count] = string.sub(str, count, count)
names[count].id = count
end
顺便说一句,这只是一个例子,但我的概念在那里。无论如何,它总是返回一个错误,即尝试索引字段“?”(一个nil值)。这个错误意味着什么?我试着在我的另一个项目中使用类似的东西,效果很好。除了,它是一个图像,为什么它不适用于这个实例?索引适用于哪里?我发现您的代码存在两个问题:
我发现您的代码存在两个问题:
这里的问题是您对如何使用默认的lua值类型感到困惑。tables和userdata是您将设置/获取属性的唯一两种数据类型。我将分解你的代码,这样也许可以帮助你理解如何使用表来做你想做的事情 首先创建一个名为names的空表。该表中没有可供参考的值或属性
local names = {}
在for循环中,每次从字符串“str”中提取一个字符,并将其分配给count所指向的索引处的名称(应该从1开始,顺便说一下,因为lua中的字符串和表索引是基于1的,而不是基于零的)。在第二个循环中,你基本上是这样做的:
names[1] = 'H'
(第一个循环计数器为0,因此string.sub(str,0,0)返回一个空字符串)
紧接着,你一次做了两个步骤,这就是你感到困惑的地方。爆发出来应该会帮你清理干净的
local a_char = names[count] -- get the string value in index 'count'
a_char.id = count -- try to set property id on that string value
names[count] = a_char -- assign this value to index 'count' in table names
上面的代码在逻辑上与名称[count].id=count等效。您正在尝试为字符串值创建/设置名为“id”的属性。字符串没有该属性,并且不允许您创建它,这就是为什么解释器对您咆哮
如果要在lua表中一起存储逻辑信息,通常使用嵌套表。听起来,您基本上希望将每个字符存储在字符串“str”中,以及它在表“names”中的位置。你应该这样做:
local names = {}
str = "Hello World"
for count = 1, #str do
local cha, idx = string.sub(str, count, count), count
-- below creates an anonymous table with two properties (character, and id) and
-- adds it to the end of table 'names'.
table.insert(names, {character = cha, id = idx})
-- or
-- names[count] = {character = cha, id = idx}
end
按照您希望的方式对信息进行逻辑分组,数据大致如下表所示:
{ {character = 'H', id = 1}, {character = 'e', id = 2} ... }
如果您想要表中第一项的id,您可以像上面那样引用它:
local first_id = names[1].id -- access property id from table in first index in table names
这里的问题是您对如何使用默认的lua值类型感到困惑。tables和userdata是您将设置/获取属性的唯一两种数据类型。我将分解你的代码,这样也许可以帮助你理解如何使用表来做你想做的事情 首先创建一个名为names的空表。该表中没有可供参考的值或属性
local names = {}
在for循环中,每次从字符串“str”中提取一个字符,并将其分配给count所指向的索引处的名称(应该从1开始,顺便说一下,因为lua中的字符串和表索引是基于1的,而不是基于零的)。在第二个循环中,你基本上是这样做的:
names[1] = 'H'
(第一个循环计数器为0,因此string.sub(str,0,0)返回一个空字符串)
紧接着,你一次做了两个步骤,这就是你感到困惑的地方。爆发出来应该会帮你清理干净的
local a_char = names[count] -- get the string value in index 'count'
a_char.id = count -- try to set property id on that string value
names[count] = a_char -- assign this value to index 'count' in table names
上面的代码在逻辑上与名称[count].id=count等效。您正在尝试为字符串值创建/设置名为“id”的属性。字符串没有该属性,并且不允许您创建它,这就是为什么解释器对您咆哮
如果要在lua表中一起存储逻辑信息,通常使用嵌套表。听起来,您基本上希望将每个字符存储在字符串“str”中,以及它在表“names”中的位置。你应该这样做:
local names = {}
str = "Hello World"
for count = 1, #str do
local cha, idx = string.sub(str, count, count), count
-- below creates an anonymous table with two properties (character, and id) and
-- adds it to the end of table 'names'.
table.insert(names, {character = cha, id = idx})
-- or
-- names[count] = {character = cha, id = idx}
end
按照您希望的方式对信息进行逻辑分组,数据大致如下表所示:
{ {character = 'H', id = 1}, {character = 'e', id = 2} ... }
如果您想要表中第一项的id,您可以像上面那样引用它:
local first_id = names[1].id -- access property id from table in first index in table names
1.既然如此,我能从2开始而不介意1吗?这也可能是原因吗?2.那么,这样的事情一旦分配了字符串就无法完成了?我说的对吗?@ronel:1
对于count=1,#str
是您需要更改的全部内容string.sub(str,0,0)
将始终返回“”
string.sub(“foo”,1,1)
将返回您在这种情况下期望的“f”
。而名称
表中的索引将从1变为str
的长度。至于2.,你可能想做一些像names[count]={};名称[count].string=string.sub(str,count,count);names[count].id=count
,它将存储一个同时包含子字符串和id的表。感谢您的回复。是的,我现在知道for循环是如何工作的了。我的主要问题是“姓名[count].id”我似乎不能这样做。它不断返回一个错误。我的目的是希望在数组的内容旁边添加标签。类似于名称[count]。别名或名称[count]。我是这样活动的。我能用这个来做吗?1.那么,我能从2开始而不介意1吗?这也可能是原因吗?2.那么,这样的事情一旦分配了字符串就无法完成了?我说的对吗?@ronel:1对于count=1,#str
是您需要更改的全部内容string.sub(str,0,0)
将始终返回“”
string.sub(“foo”,1,1)
将返回您在这种情况下期望的“f”
。还有你的索引