Jump to content




Returning number of entries not working with # operator


  • You cannot reply to this topic
8 replies to this topic

#1 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 15 May 2015 - 02:31 AM

I'm trying to get the number of entries inside a table that has entries that do not start at 1, but I keep getting 0 if the table doesn't start at 1 like normal. I am supposed to be retrieving the number of entries by doing this:
entries = #table1
But it seems that if the numbered entries don't start with 1, it won't return anything other than 0.

Can anyone help me find a way to get the number of entries in a different way?

Pic related:Posted Image

EDIT: Also, one thing to note is that I am defining this number within another table, so if there's a function I can use WITHIN the table, I would.

Edited by Cranium, 15 May 2015 - 02:35 AM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 15 May 2015 - 02:37 AM

Here, select is used to get the length of a table with nil keys (which I assume is what you are doing).

#3 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 15 May 2015 - 02:44 AM

On line 48 of my code, I try to define the number of table entries using #, but I'm not quite sure how I'd implement your suggestion into my code here. Any ideas?

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 15 May 2015 - 03:09 AM

If you want to know the highest index before you hit your first nil, you use:

#myTable  -- With your example table, 0

If you want to know the highest non-nil index "full-stop", you use:

table.maxn(myTable)  -- With your example table, 12

In most cases the above two options are interchangeable, but table.maxn() executes slower.

If you want to know the total number of non-nil indexes, you use:

local counter = 0
for key, value in pairs(myTable) do counter = counter + 1 end
-- With your example table, counter will be 1


#5 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 15 May 2015 - 03:17 AM

View PostBomb Bloke, on 15 May 2015 - 03:09 AM, said:

local counter = 0
for key, value in pairs(myTable) do counter = counter + 1 end
-- With your example table, counter will be 1
At one point, I had this in the code, and it did work to an extent if I put it elsewhere, however it seems it doesn't like being put into a table to define dynamically. I keep getting attempt to concatenate string and function, because well...that's exactly what I'm doing. But I want to concatenate a string and the result of the function instead.

Any thoughts?

#6 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 15 May 2015 - 03:37 AM

Wrap it in parentheses, then call it.

local i = (function() return 3 end)()
print( i )


#7 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 15 May 2015 - 03:40 AM

Erm... run the function?

p.getAllStacks and (function() local count = 0 for _ in pairs(p.getAllStacks()) do count = count + 1 end return count end)() or "none"

Edit: :ph34r:

Edited by Bomb Bloke, 15 May 2015 - 03:41 AM.


#8 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 15 May 2015 - 03:42 AM

Hmm, interesting. That worked, but i'll be darned if I know how or why...

EDIT: I should clarify, I'm not used to performing operations within the initial definition of a table, so I didn't know it was table = {value = (function() <do stuff>)()}

Edited by Cranium, 15 May 2015 - 03:43 AM.


#9 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 15 May 2015 - 03:50 AM

Fair 'nuff.

Personally, though, rather then redefining a new function every time I built a new table, I'd define it one single time elsewhere in the script. Eg:

local function countElements(myTable)
  local counter = 0
  for key, value in pairs(myTable) do counter = counter + 1 end
  return counter
end

.
.
.

p.getAllStacks and countElements(p.getAllStacks()) or "none"

This 1) makes the code easier to read through, and 2) makes it execute faster too.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users