Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Really basic tables question
Started by hdez Aug 26 2010 03:34 AM

- - - - -
2 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

hdez

[GLOBAL: userInfoPane.html]
hdez
  • Observer

  • 18 posts
  • Corona SDK

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
uid: 7947 topic_id: 1777 reply_id: 301777


[TOPIC: post.html]
#2

andyhullinger

[GLOBAL: userInfoPane.html]
andyhullinger
  • Enthusiast

  • 30 posts
  • Corona SDK

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


--output--
4
2
3

8
uid: 5339 topic_id: 1777 reply_id: 5272


[TOPIC: post.html]
#3

hdez

[GLOBAL: userInfoPane.html]
hdez
  • Observer

  • 18 posts
  • Corona SDK

Thanks so much. This is very clear.

hdez
uid: 7947 topic_id: 1777 reply_id: 5309



[topic_controls]
[/topic_controls]