This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Cranium's profile picture

Returning number of entries not working with # operator

Started by Cranium, 15 May 2015 - 01:31 AM
Cranium #1
Posted 15 May 2015 - 03: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:

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 on 15 May 2015 - 01:35 AM
KingofGamesYami #2
Posted 15 May 2015 - 03:37 AM
Here, select is used to get the length of a table with nil keys (which I assume is what you are doing).
Cranium #3
Posted 15 May 2015 - 03: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?
Bomb Bloke #4
Posted 15 May 2015 - 04: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
Cranium #5
Posted 15 May 2015 - 04:17 AM
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?
KingofGamesYami #6
Posted 15 May 2015 - 04:37 AM
Wrap it in parentheses, then call it.


local i = (function() return 3 end)()
print( i )
Bomb Bloke #7
Posted 15 May 2015 - 04: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 on 15 May 2015 - 02:41 AM
Cranium #8
Posted 15 May 2015 - 04: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 on 15 May 2015 - 02:43 AM
Bomb Bloke #9
Posted 15 May 2015 - 04: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.