【问题标题】:Using variable to add key's to table使用变量将键添加到表中
【发布时间】:2016-01-30 22:59:20
【问题描述】:

我正在尝试使用一个表并通过使用变量的名称作为键向它添加新数据(另一个表),并将该键添加到表中。我的第一个方法是:

local table
var = read()
print(var.." "..type(var))
table[var] = {name = var, foo = bar}

不幸的是,这会导致错误(预期索引,结果为零)。如果我输入一个字符串值,表格前面的打印行会打印一个值以及类型字符串,但它说它在表格的行中得到一个 nil。来自实际代码的更大代码 sn-p(来自 minecraft ComputerCraft mod):

m = peripheral.wrap("right")
m.clear()
m.setCursorPos(1,1)

mline = 1

bgcolor = colors.white
txtcolor = colors.black

debugbg = colors.green
debugtxt = colors.lightGreen

mainscreen = true
searchscreen = false
newitemscreen = false
newitemconfirmation = false
running = true

recipes = {}
temp_recipe = {}
 --end of variable declaration part, start of function declarations

function write(text,x,y,cl,bg) --custom writing function
  term.setCursorPos(x,y)
  term.setBackgroundColor(bg)
  term.setTextColor(cl)
  term.write(text)
  term.setBackgroundColor(bgcolor)
  term.setTextColor(txtcolor)
end

...

function newItem()
  temp_recipe = {}
  write("Name of the item: ",1,3,txtcolor,bgcolor)
  local item = read()
  write("Amount of items: ",1,4,txtcolor,bgcolor)
  local itemAmount = read()
  write("Amount of items types needet for crafting: ",1,5,txtcolor,bgcolor)
  local ingredientCount = read()
  local ingredientList = {}
  for iC = 1,ingredientCount,1 do
    write("Name of crafting component "..iC..": ",1,6,txtcolor,bgcolor)
    ingredient = read()
    write("Amount of crafting component "..iC..": ",1,7,txtcolor,bgcolor)
   ingredientAmount = read()
   ingredientList[ingredient] = {name = ingredient, amount = ingredientAmount}
  end
>>>>>   temp_recipe[item] = {name = item, amount = itemAmount, ingredients = ingredientList} -- Line that causes the error
  term.setCursorPos(1,8)
  printRecipe(temp_recipe["name"],item) -- irrelevant function to display the entered data
end

有没有办法找到解决这个问题的方法?代码在一个函数中被针刺,用于将多个数据条目分配给表,然后可以使用代表名称的键访问这些条目。

【问题讨论】:

  • read 函数是什么?

标签: lua lua-table


【解决方案1】:

您永远不会创建 table 表。所以你不能分配进去。

你想要local table = {}table = {var = {name = var, foo = bar}}

也不要使用table 作为变量名。它隐藏/隐藏table 模块。

【讨论】:

  • 名称表只是一个例子。该表是在实际代码中声明和分配的,但在函数之外。我只是忘了在代码中提及它。在编写 table = {[var] = {etc}} 时,它实际上添加了一个以 var 的值命名的键,但在尝试代码时,它似乎总是删除表的所有其他条目。我不知道这是否可能源于代码的其他功能。
  • 所以您的实际问题与您实际提出的问题完全不同?你确实意识到这完全没有帮助,对吧?您还意识到tab = {[var] = "value"} 正在创建一个 new 表并将其存储在变量中?而不是设置varin tab 对吗?因为您清楚地知道分配给表键的是tab[key]
  • 如果我写 table{var = {data}} 它将创建一个名为“var”的键并将其添加到表中。写入 tab[var] = {} 会导致预期索引错误,得到 nil。
  • tab[var] = {} 是做你想做的事情的正确方法。它会导致您在发布的代码中遇到错误,因为 table 实际上并不存在。向我们展示 real 代码和 real 错误,也许我们可以帮助解决 那个 案例而不是这个案例。
  • 这是真正的错误,表是这样声明的:dataList = {}
【解决方案2】:

要为 Lua 中的表分配新的键/值,您可以用 [] 将键括起来,并使用赋值 (=) 运算符为其分配一个值。示例:

local tab = {}
local index = 5
local value = 10
tab[index] = value

你的错误告诉我你正在分配一个 nil 索引(这不是直接可能的)。所以 read 必须返回一个 nil 值。

如果没有任何进一步的信息,我恐怕无法提供帮助。

【讨论】:

  • 就像我已经提到的,我在 read() 和表赋值之间设置的打印方法返回了一个字符串变量。然而它说它得到了一个零
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 2023-03-31
  • 2021-11-03
相关资源
最近更新 更多