Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array as value in a lua table

I couldn't find an answer to my question: Can you have an array as value in a lua table?

local colors = {"blue" = {0,0,1,1}, "green" = {0,1,0,1}, "red" = {1,0,0,1} , "orange" = {0.5, 0, 0.5, 1}, "black" = {0,0,0,1}, "gold" = {1, 215/255, 0, 1}}

I get the error on this line using the corona sdk:

'}' expected near '='

like image 980
Cing Avatar asked Oct 20 '25 14:10

Cing


1 Answers

It's tables all the way down :-) Yes, tables (including ones indexed like arrays) can be elements of tables in Lua. This chapter in the Lua manual explains the different ways of defining the elements of a table.

In your example, you should not put quotation marks around the key.

local colors = { blue = { 0, 1, ...}, green = { ... }, ... }

Or you can do:

local colors = {}
colors[ "blue" ] = { 0, ... }
colors[ "green" ] = ...

Or

colors.blue = { 0, ....  }
colors.green = ....

The latter is syntactic sugar for the other forms.

like image 179
GoojajiGreg Avatar answered Oct 23 '25 05:10

GoojajiGreg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!