This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Neander_King's profile picture

Tables within tables

Started by Neander_King, 10 June 2014 - 05:50 PM
Neander_King #1
Posted 10 June 2014 - 07:50 PM
Ok, so here is my table:

local options={
  [1]={--TechMaterials x16-29
	"Monitor"={cost=5},
	"Adv.Monitor"={cost=6},
	"Computer",
	"Adv.Computer",
	"Turtle",
	"Adv.Turtle",
	"Modem"
  },
  [2]={--ProgramDisks x31-44
	"Compressor",
	"PasswordLock",
	"Shop Api"
  },
  [3]={--QuikKits x46-61
	"Logger",
	"Quarry",
	"Mob Grinder"
  }
}

And this is probably just a lack of knowledge, but why does this return an error(BIOS:366)?
Full program: http://pastebin.com/1s1H9Y2q
Please explain if you can why "Monitor" itself cannot be a table.

Edit: Also, how would I retrieve table data from a separate file for use in my code, as opposed to having a large table inside my program?
Edited on 10 June 2014 - 06:26 PM
MKlegoman357 #2
Posted 10 June 2014 - 08:57 PM
Please, next time post the full error, with line number and the actual error.

Your table definition is bad. When you are defining table keys, inside a table constructor, as strings you can define them in two different ways:

  • enclose the string with square brackets (works with all strings):
  • 
    local myTable = {
    ["table key"] = "value";
    ["another key"] = "another value"
    }
    
  • define key names just like normal variables (works with specifically formatted keys):
  • 
    local myTable = {
    tableKey = "value";
    anotherKey = "another value"
    }
    

The easiest way of saving and reading a table to/from a file is using textutils.serialize and textutils.unserialize to convert the table to/from a string. To create and read files you would use fs API, specifically fs.open.
Edited on 10 June 2014 - 06:58 PM
Lyqyd #3
Posted 10 June 2014 - 09:00 PM
His last two tables are fine. The post above only talks about defining string keys. You can create tables with just a list of values, in which case the keys are automatically assigned as numbers 1 - n.
Neander_King #4
Posted 10 June 2014 - 09:47 PM
Thanks for your advice, but how would I reference a string key in a for loop. Do the keys still hold a numeric value if they are strings? For example, could I do:

table={
["one"]={
  "value1", "value2"
},
["two"]={
  "value3", "value4"
},
}
for i=1,#table do
print(table[i][1]
end

Which I think would print value1 and value3.
Is this the case, or is there an alternative?
Lyqyd #5
Posted 10 June 2014 - 11:12 PM
No, that would definitely not work. You could use a pairs loop, but the entries aren't guaranteed to be in any specific order. This is roughly equivalent to your example above, but using a better table name. Don't call your tables "table", as that will blow away the table API, at least within the scope you declare your table with that name.


for key, value in pairs(myTable) do
  print(value[1])
end
Neander_King #6
Posted 10 June 2014 - 11:56 PM
Thanks for your help! This should simplify my code a lot!