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

Table problems

Started by Bye., 17 June 2016 - 08:34 AM
Bye. #1
Posted 17 June 2016 - 10:34 AM
Hi, I've put this code at the top of a program but there's a problem…


colorReferences = {"1" = "0",
				  "2" = "1",
				  "4" = "2",
				  "8" = "3",
				  "16" = "4",
				  "32" = "5",
				  "64" = "6",
				  "128" = "7",
				  "256" = "8",
				  "512" = "9",
				  "1024" = "a",
				  "2048" = "b",
				  "4096" = "c",
				  "8192" = "d",
				  "16384" = "e",
				  "32768" = "f",
			   }


bios.lua:14: [string "test.lua"]:1: '}" expected

How can I fix this?
Thanks
DvgCraft #2
Posted 17 June 2016 - 10:56 AM
You should do it like this:

colorReferences = {["1"] = "0",
				  ["2"] = "1",
				  ["4"] = "2",
				  ["8"] = "3",
				  ...
			   }
Bye. #3
Posted 17 June 2016 - 11:02 AM
You should do it like this:

colorReferences = {["1"] = "0",
				  ["2"] = "1",
				  ["4"] = "2",
				  ["8"] = "3",
				  ...
			   }

Thank you!
I don't understand why I need these brackets, usually I don't use them…
Dragon53535 #4
Posted 17 June 2016 - 03:48 PM
If you usually don't use them, then you more than likely don't use quotes as well. It's part of how the Lua interpreter deals with tables. String keys, which those are, either have to be declared with no quotes and brackets, or with quotes and brackets. The exact reason behind it is something that I'm sure I know, but do not remember.
Edited on 17 June 2016 - 01:49 PM
Emma #5
Posted 18 June 2016 - 04:01 AM
–snip–
I don't understand why I need these brackets, usually I don't use them…

Ok, so this is not really necessary but I'm going to try to explain why you need to have either quotes and brackets, or neither, and in some cases you don't have a choice.

Brackets (when defining a table('s key)) specify that your going to input an exact, constant data type. A literal, if you will. This can be in the form of primitives, such as numbers or strings, or you can pass variables that contain more complex data types such as functions, or even other tables. When you do not have brackets, it assumes that your going to pass it a plain sequence of characters consisting of only characters or characters with numbers, it's basically a shortcut of using brackets and enclosing them with quotes, so, when you include quotes when trying to assign a key, it sees a quote and assumes that you're trying to set a value of an automatically numerically indexed table. So when it sees the equals sign it freaks out, because it thought that you were trying to assign a value, something that is succeeded by a comma to indicate the next entry in the table, and it errors. When you need the key to have special characters such as quotes, commas, spaces, etc. you don't have a choice, because you can't supply them without quotes and brackets because the interpreter will get confused, so you can use a literal string (and use escapes if you need to have quotes as part of your key) to define an entry.

Hope I helped. :)/>
Bye. #6
Posted 18 June 2016 - 11:40 AM
–snip–
I don't understand why I need these brackets, usually I don't use them…
Ok, so this is not really necessary but I'm going to try to explain why you need to have either quotes and brackets, or neither, and in some cases you don't have a choice.
Brackets (when defining a table('s key)) specify that your going to input an exact, constant data type. A literal, if you will. This can be in the form of primitives, such as numbers or strings, or you can pass variables that contain more complex data types such as functions, or even other tables. When you do not have brackets, it assumes that your going to pass it a plain sequence of characters consisting of only characters or characters with numbers, it's basically a shortcut of using brackets and enclosing them with quotes, so, when you include quotes when trying to assign a key, it sees a quote and assumes that you're trying to set a value of an automatically numerically indexed table. So when it sees the equals sign it freaks out, because it thought that you were trying to assign a value, something that is succeeded by a comma to indicate the next entry in the table, and it errors. When you need the key to have special characters such as quotes, commas, spaces, etc. you don't have a choice, because you can't supply them without quotes and brackets because the interpreter will get confused, so you can use a literal string (and use escapes if you need to have quotes as part of your key) to define an entry.
Hope I helped. :)/>/>

You definitely helped me! Thanks