172 posts
Location
USA
Posted 08 November 2016 - 10:34 PM
How would i create a table in lua?
I know how to make one like a = {} but what about in Lua code? something like table.new(tablename)
Thanks!
- House
2427 posts
Location
UK
Posted 08 November 2016 - 10:41 PM
{} creates a table this can be assigned to a variable name, like your a example, or anonymous, perhaps passed to a function.
There is not any other way that I'm aware of.
172 posts
Location
USA
Posted 08 November 2016 - 10:46 PM
I actually just found out that i can do
a["x"] = "Test"
then i typed a and it said
{
a = "Test",
}
EDIT: But how would i read it then?
- House
Edited on 08 November 2016 - 09:47 PM
8543 posts
Posted 09 November 2016 - 01:22 AM
You had previously declared a to be a table, then. You can't simply start placing things into any old variable without declaring it to be a table first. What are you actually trying to do? Including more details will help us clear up whatever might be confusing you, and enable us to better answer your actual question.
756 posts
Posted 09 November 2016 - 02:27 AM
This kind of information is
easily found in the CC wiki, and on other Lua guides on google.
http://www.computercraft.info/wiki/Tables
463 posts
Location
Star Wars
Posted 09 November 2016 - 01:37 PM
As Anavirns says, you can google it, these are some basics in Lua, but wanna introduce you:
local tPerson = {} -- Constructs a table
local tPreparedTable = { 1, 4, x = 15, alpha = "80*" }
-- [[
tPreparedTable contains the indexes 1 to 2.
print(tPreparedTable[1]) -- 1
print(tPreparedTable[2]) -- 4
It contains also the indexes x (value: 15) abd alpha (value (string) 80)
]]
tPerson.Anavrins= {} -- Construct in tPersons a table for Anavirns
tPerson.Anavrins.name = "Anavrins"
print(tPerson.Anavirns.name) --> Anavirns
tPerson.Anavrins.name = "is not Anavrins"
for sName, tData in pairs(tPersons) do -- Iterate through every index in tPersons and set sName to the key and tData to the value
print(sName, tData.Name) --> Anvairns is not Anavirns
end
It just give you a little, very little view of the power of tables.
172 posts
Location
USA
Posted 09 November 2016 - 08:16 PM
You had previously declared a to be a table, then. You can't simply start placing things into any old variable without declaring it to be a table first. What are you actually trying to do? Including more details will help us clear up whatever might be confusing you, and enable us to better answer your actual question.
I know how to make tables, you can just do a = {} but how do i make the
code make the table without having to manually go into the code and add it. I want to make a virtual filesystem and i want it to make a table called the file name and when the user writes. It will add that line the user wrote to the table.
463 posts
Location
Star Wars
Posted 09 November 2016 - 08:27 PM
I didn't got it. Do you want a function returning a table? Or do you want to save it onto a file?
But if you want virtual filesystems, you can use
FSector (API), i coded it exactly for that =)
Edited on 09 November 2016 - 07:27 PM
2427 posts
Location
UK
Posted 09 November 2016 - 09:10 PM
You had previously declared a to be a table, then. You can't simply start placing things into any old variable without declaring it to be a table first. What are you actually trying to do? Including more details will help us clear up whatever might be confusing you, and enable us to better answer your actual question.
I know how to make tables, you can just do a = {} but how do i make the
code make the table without having to manually go into the code and add it. I want to make a virtual filesystem and i want it to make a table called the file name and when the user writes. It will add that line the user wrote to the table.
Variable names should describe data, not be data. Having the name of the file which the user is editing
is data and therefore should not be the variable name.
If you really want this then you could use nested tables.
--# your code
openFiles = {}
fileName = "example"
openFiles[fileName] = {}
--# user code
openFiles.example[1] = "this is line 1"
openFiles.example[2] = "this is line 2"
This just looks difficult to use in my opinion.