+cantuland Posted May 10, 2009 Share Posted May 10, 2009 Is there a Lua Language call that can be used to find the number of items in an array? For example, if I have an array called ArrayStuff and the contents are {apple, banana, carrot, desert}, I want to be able to do something like: EndingNumber = NumberOfItemsInArray(ArrayStuff) for x=1,NumberOfItemsInArray do and have it go from x=1 to x=4, pulling the 4 out by counting the array items. Quote Link to comment
+sTeamTraen Posted May 10, 2009 Share Posted May 10, 2009 Lua doesn't really have arrays, it has tables. This is imprtant since the index values can be anything, not just integers. You can get the number of element in a table t with # t , but that doesn't help much if the index values are -47, 12, and "george.w.bush". You can iterate over all the elements in t with: for i,v in ipairs(t) do print(i,v) end Each time round the loop i is the index and v is the value of the corresponding element. Quote Link to comment
Ranger Fox Posted May 11, 2009 Share Posted May 11, 2009 To get an table's/array's length, I've always used table.getn(myArray) Seems to have worked fine for everything I've done so far. You'll see it done a lot in Whack-A-Lackey and some in Sadie's BiG Adventure. Quote Link to comment
+cantuland Posted May 12, 2009 Author Share Posted May 12, 2009 Thanks bunches! Those will help me out. Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.