Jump to content

Lua call for array size?


cantuland

Recommended Posts

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.

Link to comment

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.

Link to comment

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...