Jump to content




Tables within tables

computer help lua

  • You cannot reply to this topic
5 replies to this topic

#1 Neander_King

  • Members
  • 25 posts
  • LocationPryp'yat' Kyivs'ka oblast Ukraine

Posted 10 June 2014 - 06: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 by Neander_King, 10 June 2014 - 07:26 PM.


#2 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 10 June 2014 - 07: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 by MKlegoman357, 10 June 2014 - 07:58 PM.


#3 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 10 June 2014 - 08: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.

#4 Neander_King

  • Members
  • 25 posts
  • LocationPryp'yat' Kyivs'ka oblast Ukraine

Posted 10 June 2014 - 08: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?

#5 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 10 June 2014 - 10: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


#6 Neander_King

  • Members
  • 25 posts
  • LocationPryp'yat' Kyivs'ka oblast Ukraine

Posted 10 June 2014 - 10:56 PM

Thanks for your help! This should simplify my code a lot!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users