1

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.

1
  • What's the read function? Commented Oct 31, 2015 at 14:12

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

the name table was just as an example. The table is being declared and assigned in the actual code, but outside the function. I just forgot to mention it in the code. When writing table = {[var] = {etc}} it does in fact add an key named after the value of var, but when trying the code it always seems to remove all other entries of the table. I dont know if this may originate from other functions of the code.
So your actual question is entirely different then then one you actually asked? You do realize that's entirely unhelpful right? You also realize that tab = {[var] = "value"} is creating a new table and storing it in the variable? And not setting the var key in tab right? Because assigning to a table key is tab[key] as you clearly are aware.
If I would write table{var = {data}} it would create a key named "var" and add that to the table. Writing tab[var] = {} causes the error with the index expected, got nil.
tab[var] = {} is the correct way to do what you want. It causes the error you are getting in your posted code because table doesn't actually exist. Show us the real code and the real error and maybe we can help with that case instead of this one.
It is the real error, and the table is declared like this: dataList = {}
|
0

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.

1 Comment

Like I already mentioned, the print method I set between the read() and the table assignment returned a string variable. Yet it says it got a nil

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.