me.limes
me.limes

Reputation: 471

Index values from table into another table

I want to store the values by selecting the keys of a table into another table, for example:

polyline = {color="blue", thickness=2, npoints=4}

stuff = {"polyline.color":[polyline.thickness]}
print(stuff)

Should produce:

blue   2

However, I get the following error:

input:3: '}' expected near ':'

Upvotes: 1

Views: 207

Answers (2)

Piglet
Piglet

Reputation: 28950

local polyline = {color="blue", thickness=2, npoints=4}

local stuff = {polyline.color, polyline.thickness}
print(table.unpack(stuff))

Upvotes: 2

Kamiccolo
Kamiccolo

Reputation: 8497

I believe, You're mixing in some Python syntax. Do you notice using two different (wrong) ways of accessing the values?

I guess, this is what You've meant with your snippet of Lua code:

polyline = {color = "blue", thickness = 2, npoints = 4}

stuff = {[polyline.color] = polyline.thickness}
for key, val in pairs(stuff) do
    print(key, val)
end

Upvotes: 2

Related Questions