FRA32
FRA32

Reputation: 21

Using variable to add key's to table

I am trying to use a table and add new data(another table) to it by using the name of a variable as key, and adding that key to the table. My first approach was:

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

Unfortunally this causes an error(index expected, got nil). The print line in front of the table does print a value along with the type string if I enter a string value, yet it says it gets a nil in the line with the table. An bigger code snippet from the actual code(From the 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

Is there a way to find a solution around this problem? The code is needet inside a function to assign multiple data entrys to the table, which then are accessible using the key representing the name.

Upvotes: 1

Views: 2136

Answers (2)

warspyking
warspyking

Reputation: 3113

To assign new key/values to a table in Lua, you surround the key with [] and assign a value to it with the assignment (=) operator. Example:

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

Your error tells me you're assigning to a nil index (which isn't directly possible). So read must be returning a nil value.

Without any further information I am afraid I cannot help.

Upvotes: 0

Etan Reisner
Etan Reisner

Reputation: 80921

You never create the table table. So you can't assign into it.

You want local table = {} or table = {var = {name = var, foo = bar}}.

Also don't use table as a variable name. It shadows/hides the table module.

Upvotes: 1

Related Questions