Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
EASY: Length of table - how to count? — Gideros Forum

EASY: Length of table - how to count?

david19801david19801 Member
edited November 2012 in General questions
Hi, I have a table called actors:

local actors = {}

After I put things in to this how can I get a count of how many things are in it? I tried actors.length and actors.count. nothing...

Comments

  • local cnt = #actors
  • MellsMells Guru
    edited November 2012
    Google -> lua table length ;)
    local count = #actors
    Lua.org

    The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; moreover, if t[1] is nil, n can be zero. For a regular array, with non-nil values from 1 to a given n, its length is exactly that n, the index of its last value. If the array has "holes" (that is, nil values between other non-nil values), then #t can be any of the indices that directly precedes a nil value (that is, it may consider any such nil value as the end of the array).
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • @david, if you use
    table.insert
    then this works fine, however if you use names key value pairs or a zero index or a gap in the items,
    #actors
    will not return the correct length. The other way is to count them yourself by iterating through the list using
    pairs
    and if you want to count only the index type irrespective of the gaps in between, use the
    ipairs
    to iterate through the contents.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
Sign In or Register to comment.