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

Use table with key and Color API

Started by Dorendil, 24 March 2016 - 09:05 PM
Dorendil #1
Posted 24 March 2016 - 10:05 PM
Hi,
I have this error when I try to use a table with the Color API :
bios:14: [string "elevator"]:2: '}' expected

but I can't understand why

here is my code :

------ VARS ------
local maxFloor = 1
local tableFloor = {0 = colors.yellow, 1 = colors.orange}

------ FUNCTIONS ------
function getCurrentFloor()
for Floor, value in pairs(tableFloor) do
   if redstone.testBundledInput("back", value) == true then
	 return Floor
   end
end

return -1
end
function stop()
  redstone.setBundledOutput("left", 0)
end
function down()
  redstone.setBundledOutput("left", colors.yellow)
  sleep(0.5)
  stop()
end
function up()
  redstone.setBundledOutput("left", colors.orange)
  sleep(0.5)
  stop()
end
function goFloor(Floor)
  if Floor > maxFloor then
    Floor = maxFloor
  end
 
  print("Go to floor "..Floor)
 
  while getCurrentFloor() ~= Floor do
    if Floor < getCurrentFloor() then
	  down()
    elseif Floor > getCurrentFloor() then
	  up()
    end
sleep(1)

if getCurrentFloor() == -1 then
   print("ERROR")
   return
end
  end
 
  print("Arrived at floor "..Floor)
 
end

------  MAIN ------
while true do
  goFloor(1)
  sleep(5)
  goFloor(0)
  sleep(5)
end

thanks for your help
Bomb Bloke #2
Posted 24 March 2016 - 11:40 PM
local tableFloor = {[0] = colors.yellow, [1] = colors.orange}

The only time you can do without the square brackets is when using strings for your key names, and even then you can't use some special characters that way (eg spaces).
Lupus590 #3
Posted 25 March 2016 - 10:08 AM
Lua tables start from 1, you can cause issues with a bunch of the table utility functions if you start from 0.

If you start from 1 then you can drop the indexes entirely, Lua will infer the index.
Edited on 25 March 2016 - 09:10 AM
Dorendil #4
Posted 25 March 2016 - 10:30 AM
Thanks for your help, I'll try asap

EDIT:
It works fine ! Thanks again.
Edited on 25 March 2016 - 04:42 PM