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

'<name>' expected near '[' using a table as database?

Started by quique18c, 01 February 2014 - 09:29 PM
quique18c #1
Posted 01 February 2014 - 10:29 PM
Hi, i have a problem D:, im making an ATM working program with credit cards system using immimbis misc peripherals

I have a problem with the table that im using as a database for storing info(credit card number, name, pin and balance)

I have a database: (this is the default one)

.DATABASE DEFAULT

local database = {
  ["9999999999"] = {
	pin = 1014;
	balance = 100;
	name = "quique18c";
  };
  ["9999999999"] = {
	pin = 4321;
	balance = 100;
	name = "test";
  };
}

That is the database, in the ATM program, i load it with this


local file = fs.open('.database','r')
local database = textutils.unserialize(file.readAll())
file.close()

and i do changes in the fly with this


database[tostring(srandom)] = {
pin = 0;
balance = 0;
name = sinput;
} -- I create a new entry
database[tostring(srandom)].balance = 0
database[tostring(srandom)].pin = tonumber(input2) -- i change the values
database[tostring(srandom)].name = sinput

then, i save the database with this


local file = fs.open('.database','w')
file.write(textutils.serialize(database))
file.close()

My question is why i get this error: :176: '<name>' expected near '['
Im creating a new entry with database[tostring(srandom)] (srandom is math.random(0000000000, 9999999999), redefined at the start of the program, here is the complete program:)


local reader = peripheral.wrap("left")
local modem = peripheral.wrap("right")
local printer = peripheral.wrap("bottom")
local cmdblock = peripheral.wrap("back")
local vsnm = "0.4"
os.pullEvent = os.pullEventRaw
local function cprint(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
print(text)
end
local function cwrite(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
write(text)
end
local function csp(text, var1)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
textutils.slowPrint(text, var1)
end
function clear()
term.clear()
term.setCursorPos(1, 1)
end
local function dateprint(text)
print(os.day()..":"..os.time().." -"..text)
end
local function wite(number, name)
reader.setInsertCardLight(true)
repeat
  reader.beginWrite(number, name)
  local event = os.pullEvent()
  if event == "mag_write_done" then
   reader.setInsertCardLight(false)
   cprint("Done")
   cprint("Card data:")
   cprint(number.." - "..name)
   sleep(3)
   clear()
   startup()
  end
until event == "mag_write_done"
end
function startup()
local file = fs.open('.database','r')
local database = textutils.unserialize(file.readAll())
file.close()
if term.isColor() == true then
  term.setTextColor(32)
end
local srandom = math.random(1000000000, 9999999999)
cprint("=================================")
cprint("======Quique's ATM V."..vsnm.."======")
cprint("=================================")
cprint("What do you want?")
cprint("1 - Make a new credit card")
cprint("2 - Check balance")
cprint("3 - Withdraw money")
cprint("4 - Deposit money - comming soon")
cprint("5 - Transfer money - comming soon")
cwrite("Option:")
finput = read()
if finput == "1" then
  cprint("Selected option 1")
  sleep(0.5)
  clear()
  uno()
elseif finput == "2" then
  cprint("Selected option 2")
  sleep(0.5)
  clear()
  dos()
elseif finput == "3" then
  cprint("Selected option 3")
  sleep(0.5)
  clear()
  tre()
elseif finput == "4" then
  cprint("Selected option 4")
  sleep(0.5)
  clear()
  cua()
elseif finput == "5" then
  cprint("Selected option 5")
  sleep(0.5)
  clear()
  cin()
else
  cprint(finput.."Isnt an option!")
  sleep(0.5)
  clear()
  startup()
end
end
function uno()
cprint("Important info, read ALL LINES")
cprint("Before making a new credit card, craft one")
cprint("If u have a blank credit card now, type next")
cprint("If not, type cancel to cancel operation")
tinput = read()
if tinput == "next" then
  clear()
  uno2()
elseif tinput == "cancel" then
  cprint("Operation canceled")
  sleep(1)
  clear()
  startup()
else
  cprint(tinput.."isnt an option!")
  sleep(0.5)
  clear()
  uno()
end
end
function uno2()
cprint("You have a credit card.")
cprint("Please enter your playername")
cprint("DO NOT FAIL THIS")
cprint("Or type cancel to cancel")
cprint("Example: quique18c")
cwrite("Name:")
sinput = read()
if sinput == database then
  cprint("Already used.")
  sleep(1)
  clear()
  uno2()
elseif sinput == "cancel" then
  cprint("Operation canceled")
  sleep(0.5)
  clear()
  startup()
else
  cprint("Name: "..sinput)
  cprint("Now, type your pin (MAX 4 DIGITS)")
  input2 = read()
  if input2 == tonumber(input2) > 9999 then
   cprint("4 digits max, please")
   sleep(1)
   clear()
   uno2()
  else
   clear()
   cprint("Name: "..sinput)
   cprint("Pin: "..input2)
   cprint("Please, enter your card")
   database[tostring(srandom)] = {
	pin = 0;
	balance = 0;
	name = sinput;
   }
   database[tostring(srandom)].balance = 0
   database[tostring(srandom)].pin = tonumber(input2)
   database[tostring(srandom)].name = sinput
   local file = fs.open('.database','w')
   file.write(textutils.serialize(database))
   file.close()
   wite(srandom, sinput)
  end
end
end
function dos()
cprint("Please, enter your card")
repeat
reader.setInsertCardLight(true)
local event,arg1,arg2 = os.pullEvent()
if event == "mag_swipe" then
  if tostring(arg1) == database then
   cprint("Balance:")
   cprint("$"..database.[tostring(arg1)].balance)
   sleep(2)
   reader.setInsertCardLight(false)
   clear()
   startup()
  else
   cprint("Invalid Card")
   sleep(1)
   reader.setInsertCardLight(false)
   clear()
   startup()
  end
end
until event == "mag_swipe"
end
function tre()
cprint("Please insert your card")
repeat
reader.setInsertCardLight(true)
local event,arg1,arg2 = os.pullEvent()
if event == "mag_swipe" then
nmb = tostring(arg1)
  if nmb == database then
   reader.setInsertCardLight(false)
   clear()
   cprint("Welcome, "database.[nmb].name)
   cprint("Insert Your PIN")
   cwrite("Pin:")
   jinput = read("*")
   if jinput == database.[nmb].pin then
	cprint("Valid Pin!")
	sleep(1)
	clear()
	cprint("How much money do you want to withdraw?")
	cwrite(" ")
	kinput = read()
	if tonumber(kinput) > database.[nmb].balance then
	 cprint("U cant withdraw money that you dont have!")
	 sleep(2)
	 clear()
	 startup()
	elseif tonumber(kinput) < 0 then
	 cprint("U cant withdraw negative money!")
	 sleep(1)
	 clear()
	 startup()
	else
	 cprint("Operation in progress")
	 csp("#==================#", 4)
	 sleep(5)
	 database.[nmb].balance = database.[nmb].balance - tonumber(kinput)
	 cmdblock.setCommand("/eco give @p "..kinput)
	 cmdblock.runCommand()
	 cmdblock.setCommand(" ")
	 cprint("Operation completed")
	 local file = fs.open('.database','w')
	 file.write(textutils.serialize(database))
	 file.close()
	 sleep(2)
	 clear()
	 startup()
	end
   else
	cprint("Invalid Pin!")
	sleep(1)
	clear()
	startup()
   end
  else
   cprint("Invalid Card")
   sleep(1)
   reader.setInsertCardLight(false)
   clear()
   startup()
  end
end
until event == "mag_swipe"
end
function cua()
cprint("Comming soon")
cprint("Quique's ATM updates automatically, dont worry")
cprint("Current version:" ..vsnm)
sleep(2)
clear()
startup()
end
function cin()
cprint("Comming soon")
cprint("Quique's ATM updates automatically, dont worry")
cprint("Current version:" ..vsnm)
sleep(2)
clear()
startup()
end
Bomb Bloke #2
Posted 01 February 2014 - 10:34 PM
Near line 176 is this:

   cprint("$"..database.[tostring(arg1)].balance)

Note the extra full stop between "database" and "[tostring(arg1)]".
quique18c #3
Posted 01 February 2014 - 10:46 PM
Near line 176 is this:

   cprint("$"..database.[tostring(arg1)].balance)

Note the extra full stop between "database" and "[tostring(arg1)]".

Thanks a lot man for the fast response :)/> , it was a little derpy from my part :P/>
quique18c #4
Posted 01 February 2014 - 10:57 PM
i have a new problem, when i start the program atm, it doesnt display any error, it doesnt have any error
it just dont load..

is the "ram" of the computer full?
Lyqyd #5
Posted 01 February 2014 - 11:02 PM
The "ram" isn't full unless your physical computer's is, and that wouldn't result in it not displaying anything. Maybe put in some debug prints to see what it's doing?
quique18c #6
Posted 02 February 2014 - 09:30 AM
The "ram" isn't full unless your physical computer's is, and that wouldn't result in it not displaying anything. Maybe put in some debug prints to see what it's doing?

i have 8gb ram in my pc, 4 for minecraft, how do i activate the debug mode for CC?
MKlegoman357 #7
Posted 02 February 2014 - 10:54 AM
One problem is how you save your table. You wrote: local database = {. Get rid of that part, leave it like this:


{
  ["9999999999"] = {
        pin = 1014;
...

Why? This is how textutils.unserialize works: it appends 'return ' to the beginning of the serialized table (string) and then loads and runs that string with loadstring function. In your case it tried to load a string like this:


return local database = {
...
}

and that is an invalid Lua code. After the fix it should try to load a string like this:


return {
...
}

Sidenote: By knowing how textutils.unserialize works you can do a lot more things with it, like calculating math inputted by the user:


print("Enter some math!")

local input = read() --// take anything the user inputted to the computer
local output = textutils.unserialize(input) --// try to run the input

if type(output) == "number" then --// if the result from un-serialization is a number, then math is correct :D/>/>/>
  print("The answer is: " .. output)
else
  print("Your math is wrong!")
end

If I would run the above code and enter this (5+6)/2*math.pi it would say:
The answer is: 17.278759
Edited on 02 February 2014 - 09:56 AM
Lyqyd #8
Posted 02 February 2014 - 02:16 PM
The "ram" isn't full unless your physical computer's is, and that wouldn't result in it not displaying anything. Maybe put in some debug prints to see what it's doing?

i have 8gb ram in my pc, 4 for minecraft, how do i activate the debug mode for CC?

There isn't a debug mode. You just stick some print("here I am in the code") sort of statements in at various places to see where the code gets to and where it fails. You can nail down the exact problem that way.