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

Tables and how to use them...

Started by DannySMc, 04 July 2014 - 05:45 PM
DannySMc #1
Posted 04 July 2014 - 07:45 PM
I am looking to learn how to edit tables in a way that allows you to add, edit, and remove entries…?
I can use tables in a sense like:

local tabFile = {"Hello", "How", "Are", "You"}
for _, line in ipairs(tabFile) do
   print(line)
end

-- or:

print(tabFile[1])
print(tabFile[2])
print(tabFile[3])
print)tabFile[4])

and:

local tabFile = {["Hello"]="Bonjour", ["Love"]="sucks", ["lol"]="fail"}
print(tabFile.Hello)
print(tabFile.Love)
print(tabFile.lol)
-- or can use variables
local var1 = "Hello"
print(tabFile[var1])

I just don't know how to manipulate and add to them without having to rewrite the whole table again…?
Please don't send me to links, I really don't understand, can someone just attempt to explain for me?
Thanks
Edited on 04 July 2014 - 05:46 PM
Lyqyd #2
Posted 04 July 2014 - 07:51 PM
Links are great, though! There are excellent resources out there, like the PIL page on the table type, the PIL chapter on the table library, the Lua Reference Manual section on the table library, the lua-users wiki table tutorial, and the lua-users wiki table library tutorial. It would really be best to read through those and then ask questions about anything you don't understand from those pages. It's going to be a lot easier for us to clarify anything you don't understand than to try to explain tables from scratch.
DannySMc #3
Posted 04 July 2014 - 08:16 PM
Links are great, though! There are excellent resources out there, like the PIL page on the table type, the PIL chapter on the table library, the Lua Reference Manual section on the table library, the lua-users wiki table tutorial, and the lua-users wiki table library tutorial. It would really be best to read through those and then ask questions about anything you don't understand from those pages. It's going to be a lot easier for us to clarify anything you don't understand than to try to explain tables from scratch.

Okay so I have read through them. I get there is a table.insert() and table.remove() functions, but how do you query the database?
For example, I want to create a table with user data for my server, this will contain their username, balance, password (encrypted), houseID, jobID. Now using this method I would have to make a new table for each user. with the following details:

{
   ["username"]="testuser",
   ["password"]="testpass",
   ["balance"]=10000.
   ["houseID"]=42,
   ["jobID"]=6,
}
and make a database file for each user? how could I make it all together? for example:

{
   ["user1"]={
	  ["password"]="HelloWorld",
	  ["balance"]=10000,
	  ["houseID"]=42,
	  ["jobID"]=6,
   },
   ["user2"]={
	  ["password"]="HelloPerson",
	  ["balance"]=100000,
	  ["houseID"]=25,
	  ["jobID"]=13,
   },
}

and how would i query that? so the first table I am guessing you can query it like:

-- table is called users
strUser = "user2"
if users[user2] then
   --do whatever
else
   -- returns false
end
Would the above work? and how would you get the data from the second table I made? and how would I create it? insert and remove entries from it?
DannySMc #4
Posted 04 July 2014 - 08:44 PM
Links are great, though! There are excellent resources out there, like the PIL page on the table type, the PIL chapter on the table library, the Lua Reference Manual section on the table library, the lua-users wiki table tutorial, and the lua-users wiki table library tutorial. It would really be best to read through those and then ask questions about anything you don't understand from those pages. It's going to be a lot easier for us to clarify anything you don't understand than to try to explain tables from scratch.

Also how do you write the table to a file? like so how would i add entries? using a file instead? as when I tried to write it to a file, the file just contained: {} and nothing else? and how do you use strings instead of keys?
KingofGamesYami #5
Posted 04 July 2014 - 08:49 PM
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
Edited on 04 July 2014 - 06:50 PM
DannySMc #6
Posted 04 July 2014 - 09:16 PM
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")
Edited on 04 July 2014 - 07:18 PM
DannySMc #7
Posted 04 July 2014 - 09:24 PM
Also how do you get a table from a file back into use? I can print(table) but I can't loop it or add/modify it?
KingofGamesYami #8
Posted 04 July 2014 - 10:11 PM

local file = fs.open( "table", "r" )
local data = file.readAll()
local tbl = textutils.unserialize( data )

Also, why the **** would you want to make a "table manipulation" api? It's so much shorter to just DO it.
Edited on 04 July 2014 - 08:12 PM
DannySMc #9
Posted 04 July 2014 - 10:29 PM

local file = fs.open( "table", "r" )
local data = file.readAll()
local tbl = textutils.unserialize( data )

Also, why the **** would you want to make a "table manipulation" api? It's so much shorter to just DO it.

I don't know how to make nested tables, so an api would be easier for me..
KingofGamesYami #10
Posted 04 July 2014 - 11:41 PM
Simplicity:

local tbl = {} --#create a table
tbl.tbl = {} --#create a "nested" table

Also:

local tbl = {}
tbl[ "tbl" ] = {}
Edited on 04 July 2014 - 09:42 PM