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

Pulling and adding Variables from a table

Started by Drtrider, 12 April 2014 - 05:13 AM
Drtrider #1
Posted 12 April 2014 - 07:13 AM
Hey guys, I'm looking to use 3 sets of tables, fill them with data, and then pull from these tables and so some calculations. However, I've read everything I can seem to muster up, and for some reason I'm getting a "attempted to add null and digit" error with the following code…


local function energyChangeRate()
count = count + 1
if count > 5 then count = 1 end

local chgValue = {ch1, ch2, chg3, chg4, chg5}
local chgValue2 = {total1, total2, total3, total4, total5}
local chgAvgTotal = {avg1, avg2, avg3, avg4, avg5}

chgValue[count] = energyLastScan
chgValue2[count] = levelTotal

print("count: "..count)
print("chgValue:"..chgValue[count])
print("chgValue2:"..chgValue2[count])
 
chgAvgTotal[count] = (chgValue2[count]) - (chgValue[count])
print("chgAvgTotal:"..chgAvgTotal[count])

if (chgAvgTotal[1]) and (chgAvgTotal[2]) and (chgAvgTotal[3]) and (chgAvgTotal[4]) and (chgAvgTotal[5]) ~= null then
  totalEnergyChangeAverage = (chgAvgTotal[1] + chgAvgTotal[2] + chgAvgTotal[3] + chgAvgTotal[4] + chgAvgTotal[5]) / 5
else
  totalEnergyChangeAverage = 0
  clearLine(1,15)
  printMon(totalEnergyChangeAverage.." RF/s", 1,15,colors.white,colors.black)
end
end

Any insight would be greatly appreciated. I'm hitting a wall here as far as other options to look into!
CometWolf #2
Posted 12 April 2014 - 10:15 AM

count = count + 1
Where's count defined? Hell, where is 90% of your variables defined lol

Also, include the line number next time.
Drtrider #3
Posted 12 April 2014 - 03:35 PM
Where's count defined? Hell, where is 90% of your variables defined lol

Also, include the line number next time.

It's a somewhat lengthy code, this is simply one function that I'm having issues with, quite a few of the variables are defined eairler in the code. Here's the pastebin: http://pastebin.com/QtTzr6kX

Thanks for the reply by the way, I've been hitting my head against a wall here trying to figure out what the issue could be.
Bomb Bloke #4
Posted 13 April 2014 - 12:39 AM
Can't see anything in there that'd trigger that error. Nor can I see that line number from you yet, either. :P/>

I'll assume that your filling of chgValue / chgValue2 / chgAvgTotal with nil values is a placeholder for something, but I can tell you that you likely don't want to define those tables as local to energyChangeRate(). They'll be wiped every time it finishes.

And this block here:

        while count2 < 5 do --Checks each value for a null, if found sets it to 1. May slightly skew data when first starting up.
                if  count2 == 5 then
                        count2 = 0
                end
                count2 = count2 +1
                if chgAvgTotal[count2] == null then
                        chgAvgTotal[count2] = 1
                end    
        end

… could be condensed:

	for i=1,5 do
		if not chgAvgTotal[i] then chgAvgTotal[i] = 1 end
	end

Or even:

	for i=1,5 do chgAvgTotal[i] = chgAvgTotal[i] or 1 end

You can likely see how to integrate your energyAvg totalling into that loop, if you wanted to.