Cing
Cing

Reputation: 816

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

Upvotes: 4

Views: 7203

Answers (1)

GoojajiGreg
GoojajiGreg

Reputation: 1183

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.

Upvotes: 5

Related Questions