Odd ComputerCraft/Lua Problem

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here
  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

voidreality

New Member
Jul 29, 2019
117
0
0
Maybe one of you lua gurus out there can help me out. I have the following code.
Code:
local uniqueblocks={}
local uCount = 0
for i = 1, #blocks do
    idx = tonumber(blocks[i][1])  -- ANY INDEXED VALUE
    if uniqueblocks[idx] == nil then
        uniqueblocks[idx] = 0
        uCount = uCount + 1
    end
    uniqueblocks[idx] = uniqueblocks[idx] + 1
end
 
print("number of block types: " .. uCount)
print("number of block types: " .. table.getn(uniqueblocks))
print("number of block types: " .. #uniqueblocks)
print(uniqueblocks[0])
print(uniqueblocks[35])
print(uniqueblocks[85])

This code outputs:
Code:
number of block types: 3
number of block types: 0
number of block types: 0
113
25
12

I am wondering why the two array counting methods are not returning the correct count (should equal uCount, in this case 3). I read something somewhere that you can't have nil values in arrays in lua. I'm guessing this is the reason since uniqueblocks is indexed with integers, but some integers are not present. I don't know for sure.

Anyone know?
 

Bomb Bloke

New Member
Jul 29, 2019
612
0
0
Yes, nil values can be stored in tables.

#blocks returns the length of the "blocks" table, but pretends all indexes after (and including) the first one to contain "nil" don't exist.

table.getn(blocks) returns the highest populated index of the table, such that if all the indexes other then the fifth are nil, it'll still return five.

Eg,

Code:
a = {}
a[1] = "asdf"
a[3] = "fdas"

"print(#a)" will output 1. "print(table.getn(a))" will output 3. The actual amount of non-nil indexes is 2.

It's difficult to comment further without seeing how "blocks" has been constructed, but I suspect that the above answers your question.
 

voidreality

New Member
Jul 29, 2019
117
0
0
Thanks for the reply. Is there a method that gets or stores the non-nil index value of the array? If not, I guess I'll have to save the index count as I go. Which I did in this case with "uCount".

In searching (which I try to do before asking questions), I found this page. Gotcha 6.4 is what's effecting me in this case, but the page doesn't go about explaining how to fix this or work around this "problem" :(
http://www.luafaq.org/gotchas.html
 

Bomb Bloke

New Member
Jul 29, 2019
612
0
0
Your counter is indeed the most efficient solution.

You could alternatively create a function:

Code:
function usedIndexes(sometable)
  local usedIndexCount = 0
  for k,v in pairs(sometable) do usedIndexCount = usedIndexCount + 1 end
  return usedIndexCount
end

Calling "usedIndexes(a)" (where "a" is the sample table from my previous post) would return 2. However, again, since you're scanning through your table (counting duplicate blocks) anyway, you may as well do your counting then and there.
 

voidreality

New Member
Jul 29, 2019
117
0
0
Thanks again, you are very helpful :) It's good to know these lua "oddities" since my turtle code is only getting more complicated.