You can set/insert/remove entries from a table by treating them (somewhat) like variables
local tbl = {}
--#creating keys
tbl[ 1 ] = "hello"
tbl[ 2 ] = "goodby"
--#modifying keys
tbl[ 1 ] = "goodby"
tbl[ 2 ] = "hello"
--#making a key a table
tbl.anothertbl = {}
--#making keys in a table in a table
tbl.anothertbl[ "key" ] = "value"
tbl[ "anothertbl" ].key = "value"
tbl[ "anothertbl" ][ "key" ] = "value"
tbl.anothertbl.key = "value"
--#of course, you can insert tables as well
table.insert( tbl, "key", {} ) --#creates tbl["key"] as a fresh table
table.insert( tbl, {} ) --#inserts a new table at the next unoccupied numerical key
Writing a table to a file:
local file = fs.open( "table", "w" )
file.write( textutils.serialize( tbl ) )
file.close()
You most likely did:
file.write( "{}" )
strings can be keys. keys can be strings. keys can be numbers.
local tbl = {}
tbl[ 1 ] = "hi" --#a numerical key
tbl[ "hello" ] = "hi" --#a string key
tbl.hello = hi --#a cool looking way of using a string key
how can I make this like a function/api? Because I just need a simple:
-- Create table
api.table.create(tableName)
-- Insert entry
api.table.insert(tableName, [position], value)
-- Remove entry
api.table.remove(tableName, [position])
-- modify an entry
api.table.modify(tableName, position, newValue)
-- query it for a value:
api.table.query(tableName, [position], value)
-- retrieve a value from position:
api.table.retrieve(tableName, position)
The query should return with a boolean, true (if it exists) and false (if it doesn't exist).
The retrieve function should be able to retrieve a value from the position given. example:
api.table.retrieve("table1", "username")
-- this will then return:
TheBestUser
-- The above "user" is an example!
This should also be usable on sub-tables too!
The create, insert, remove and modify should be able to be used on sub-tables in a table. So for example I could use:
api.table.create("table1.table2")
or something like that? so to insert an entry:
api.table.insert("table1.table2", "username", "TheBestUser")
-- and to modify I can simply just do:
api.table.modify("table1.table2", "username", "TheWorstUser")
-- and to remove:
api.table.remove("table1.table2", "username")