Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Sequence in Gideros Studio Output window — Gideros Forum

Sequence in Gideros Studio Output window

DikkesnoekDikkesnoek Member
edited August 2012 in General questions
Hi,

Still learning Lua. I tried the for loop with a keypairs combination to understand. I tried this:
local testTable = {}
 
for i = 89, 100 do
	test = string.format("%d", i - 87)
	testTable[test] = i
end
 
for key, value in pairs(testTable) do
	print(key, value)
end
The output window will give the following values:
3 90
2 89
5 92
4 91
7 94
6 93
9 96
8 95
13 100
12 99
11 98
10 97

I expected something like:
2 89
3 90
4 91
5 92
...

Isn't this weird?

Regards,

Marc


Comments

  • That's something to do with Lua. The pairs() function in my understanding will not necessarily return key/value in any specific order, which kind of makes sense since a key could be anything from a table, function, etc.

    If you are using normal numerical indexes though starting with 1, ipairs() should return them in order.

    Likes: atilim

    +1 -1 (+1 / -0 )Share on Facebook
  • john26john26 Maintainer
    Personally I don't use the advanced "for in" loop just the basic for loop. Then you can index directly (provided your keys are numbers, as in your example):
    for i=1,10 do
      a[i]=i+1
    end
     
    for i=1,10 do
      print (i,a[i])
    end
    This old fashioned method gives you complete control and (IMO) is much more readable.
  • Actually it's also faster and more efficient as well, guess us old timers still have a few things to teach you young whippersnappers :)

    Likes: talis

    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
    +1 -1 (+1 / -0 )Share on Facebook
  • I like the old style too but I think that handling objects with keys can be powerful like I've seen in Objective-C. For simple loops you can use the standard form.

    Marc
Sign In or Register to comment.