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

[SOLVED]Named tables inside of tables with table.insert()

Started by TheeIntrepidGamer, 30 January 2014 - 05:33 AM
TheeIntrepidGamer #1
Posted 30 January 2014 - 06:33 AM
So I've been planning to work on a simple banking program, with a server and client setup to practice with tables, and rednet protcol. I was making a bit of test code, and I've seem to have come across an issue working on it. I'm trying to have a table named after the account that would hold, pin numbers, account ids, and account names, be added to the main table or "database". It seems no matter how much I messed with the broken line of code it wouldn't work. I already manually added in a default user into the table to play around with it, and table.insert would just add another user into the banking server "database". I'm newer to tables, and need a way to make this work correctly.

Pastebin: http://pastebin.com/whU97UiN

t = {
  Firestorm002 = {pin = 1234, accid = 123456789, usrname = "Firestorm002"}
}
term.clear()
term.setCursorPos(1,1)
sleep(1)
term.write("Pin#: ")
local pinnumber = read()--Pin number would be entered through an input.
local accid = math.random(1,16)
local NewUser = "Bob"
--Throws Error Code:<bios:339: [string "tables]:16: ')' expected>
---[[
table.insert(t, NewUser = {pin = pinnumber, accid = accountid, name = NewUser})
--]]
h = fs.open("table.db","w")
sTable = textutils.serialize(t)
h.write(sTable)
h.close()
Edited on 31 January 2014 - 02:09 AM
theoriginalbit #2
Posted 30 January 2014 - 06:39 AM
when inserting values under a key instead of an index you cannot use table.insert, you must instead do it like so


t[NewUser] = {pin = pinnumber, accid = accountid, name = NewUser})

basically what the program is trying to do in your code is make a new table and insert it into the variable NewUser, and then insert that into the table; Lua does not allow for this, hence the error.
Edited on 30 January 2014 - 05:41 AM
TheeIntrepidGamer #3
Posted 30 January 2014 - 06:23 PM
Works great thank you very much. :)/>