Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Associative table order
Started by richard11 Sep 02 2018 02:36 AM

- - - - -
1 reply to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

richard11

[GLOBAL: userInfoPane.html]
richard11
  • Contributor

  • 209 posts
  • Corona SDK

Hmm. Not sure if I've misunderstood something in how Lua tables work, or if this is a bug in the Corona daily build, but in trying to debug something I've put together the following test:

local test = {}
test["G"] = 1
test["W"] = 2
test["V"] = 3
test["R"] = 4

for k,v in pairs(test) do
	print(k .. " - " .. v)
end

I expected that the iteration would either return the pairs in the order they were created in, or alphabetically by key. Instead the result is as follows:

R - 4
G - 1
W - 2
V - 3

Is this expected behaviour? If so, what order are tables iterated when using pairs() and is there any way to force iteration by order created?



[TOPIC: post.html]
#2

richard11

[GLOBAL: userInfoPane.html]
richard11
  • Contributor

  • 209 posts
  • Corona SDK

Never mind - discovered this is just normal Lua behaviour and that maintaining a separate numeric table for ordering is the accepted approach.

 

Sometimes Lua still baffles me!




[topic_controls]
[/topic_controls]