Really basic tables question

I’m used to Javascript and the ability to create arrays and objects like so:

var bar = [];  
var currentBeats;  
  
bar[1] = {beats:4, subdivisions:2};  
bar[2] = {beats:2, subdivisions:2};  
bar[3] = {beats:3, subdivisions:2};  
  
--Access or set an index  
currentBeats = bar[3].beats --returns 3  

Sorry for the simplicity of this, but I’m a bit stuck: how do you pull this off in Lua? I’ve attempted this to no success:

local bar = {}  
table.insert(bar["beats"], 4)  
table.insert(bar["beats"], 2)  
table.insert(bar["beats"], 3)  

thx for any help,

hdez
[import]uid: 7947 topic_id: 1777 reply_id: 301777[/import]

I too am a luanoob, but had a similar issue. I wanted to store a “table of arrays” Lua’s combination of key=value pairs and dot syntax make tables really easy and flexible. I found these links really helpful.

http://lua.gts-stolberg.de/en/table.php
http://lua-users.org/wiki/TablesTutorial
[lua]local bar = {
{beats = 4, subdivisions = 4},
{beats = 2, subdivisions = 8},
{beats = 3, subdivisions = 2},
}

for i in pairs (bar) do
print (bar[i].beats)
end

print (bar[2].subdivisions)[/lua]

–output–
4
2
3

8 [import]uid: 5339 topic_id: 1777 reply_id: 5272[/import]

Thanks so much. This is very clear.

hdez [import]uid: 7947 topic_id: 1777 reply_id: 5309[/import]