Jump to content




Table problems

help

5 replies to this topic

#1 Bye.

  • Members
  • 59 posts

Posted 17 June 2016 - 09: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

#2 DvgCraft

  • Members
  • 14 posts
  • LocationThe Netherlands

Posted 17 June 2016 - 09:56 AM

You should do it like this:
colorReferences = {["1"] = "0",
				  ["2"] = "1",
				  ["4"] = "2",
				  ["8"] = "3",
				  ...
			   }


#3 Bye.

  • Members
  • 59 posts

Posted 17 June 2016 - 10:02 AM

View PostDvgCraft, on 17 June 2016 - 09:56 AM, said:

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...

#4 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 17 June 2016 - 02: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 by Dragon53535, 17 June 2016 - 02:49 PM.


#5 Emma

  • Members
  • 216 posts
  • Locationtmpim

Posted 18 June 2016 - 03:01 AM

View PostChecco2212, on 17 June 2016 - 10:02 AM, said:

--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. :)

#6 Bye.

  • Members
  • 59 posts

Posted 18 June 2016 - 10:40 AM

View PostIncinirate, on 18 June 2016 - 03:01 AM, said:

View PostChecco2212, on 17 June 2016 - 10:02 AM, said:

--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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users