local data = ""
local ctrl = sensors.getController()
os.unloadAPI("sensors")
os.loadAPI("/rom/apis/sensors")
function printDict(data)
for i,v in pairs(data) do
print(tostring(i).." - "..tostring(v))
end
end
function sum(data)
local sum = 0
for k,v in pairs(data) do
sum = sum + v
end
return sum
end
data = sensors.getSensors(ctrl)
Reactor = data[1]
data = sensors.getProbes(ctrl, Reactor)
ReactorInv = data[5]
data = sensors.getAvailableTargetsforProbe(ctrl, Reactor, ReactorInv)
ReactorContent = data[1]
data = sensors.getSensorReadingAsDict(ctrl, Reactor, ReactorContent, ReactorInv)
for i = 1, #data do
local amount, item, slot = string.match( data[i], pattern )
if item == "item.ice" then
table.insert(tableIce, amount)
end
end
write(" Ice: ")
data = tableIce
print(sum(data))
local pattern = "^(%d+)%*(%a+%.?%a+)@(%d+)"
local g = 0
local h = 1
while g ~= h do
local tableUranium = {}
local tableIce = {}
local tableBucket = {}
data = sensors.getSensorReadingAsDict(ctrl, Reactor, ReactorContent, ReactorInv)
printDict(data)
term.clear()
term.setCursorPos(2,2)
for i = 1, #data do
local amount, item, slot = string.match(data[i], pattern)
if item == "item.itemCellUran" then
table.insert(tableUranium,amount)
end
if item == "tile.ice" then
table.insert(tableIce,amount)
end
if item == "item.bucketWater" then
table.insert(tableBucket,amount)
end
print(tostring( item ).." "..tostring(amount))
end
print("Amount")
write(" Uranium: ")
data = tableUranium
print(sum(data))
write(" Ice: ")
data = tableIce
print(sum(data))
write(" Buckets: ")
data = tableBucket
print(sum(data))
end
(im new to forums)
So what im attempting to do is find out how many uranium cells there are in the reactor and how much ice, and if it is incorrect do something. To start with im trying to get it to display how many there are, i have Ice in the first slot, Uranium in the second and water bucket in the third. There are many parts i dont understand about how this code should be working such as the patter string and the two functions, sum and printDict.
when i run the program i get this:
Ice: reactor:15 bad argument: table expected, got nil
I know that if i print the amount and item i get nil for both no matter how i run it, any ideas pro's? xD