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

Matching table values with a wildcard?

Started by HighMans, 01 June 2015 - 12:33 PM
HighMans #1
Posted 01 June 2015 - 02:33 PM
As I understand it, the code below searches in a table for an item called, "1*item.itemCellUranDepleted@0" and then turns the function "hasEmptyCell()" to true. My question is, how do I search for all items that start with "1*item.itemCellUranDepleted@" and still make the function true?

For example "1*item.itemCellUranDepleted@1231231", "1*item.itemCellUranDepleted@2342", etc. would all make the function true not just "1*item.itemCellUranDepleted@0"


os.loadAPI("/rom/apis/sensors")
while true do
local function hasEmptyCell()
		local controllerSide, sensor, probes = sensors.getController(), "Sensor", "InventoryContent"
		for _, target in pairs( sensors.getAvailableTargetsforProbe( controllerSide, sensor, probes ) ) do
				local data = sensors.getSensorReadingAsDict( controllerSide, sensor, "Nuclear Reactor,-426,64,-715", probes )
				for i=1, table.maxn(data) do
						if data[i] == "1*item.itemCellUranDepleted@0" then
								return true
						end
				end
		end
		return false
end
if hasEmptyCell() then found = true else found = false end
print( found )
if found == true then
rs.setOutput("front", false) else
rs.setOutput("front", true)
end
sleep(1)
end
Creator #2
Posted 01 June 2015 - 02:37 PM
Maybe use gmatch and patterns?
Bomb Bloke #3
Posted 01 June 2015 - 02:55 PM
Personally I'd just use string.sub() for this;

if data[i]:sub(1, 28) == "1*item.itemCellUranDepleted@" then

This gets only the first 28 characters of data, ignoring the rest of it.This article explains more about it, along with many other operations you can perform on strings.
HDeffo #4
Posted 01 June 2015 - 04:56 PM
I have always used table.concat to find values for example


return not not table.concat(data,","):find("1*item.itemCellUranDepleted@")
Edited on 01 June 2015 - 02:57 PM