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

check table for something that is NOT there

Started by Dustmuz, 01 October 2015 - 09:54 PM
Dustmuz #1
Posted 01 October 2015 - 11:54 PM
im trying to get my computer to check my Railcraft tanks for the content..

but im running into the problem when using multiple tanks stacked, that the program crashes when one of them is empty, because the info im pulling, is no longer valid..

how do i put in a check in a table to see if a certain string is available??
Lupus590 #2
Posted 02 October 2015 - 12:12 AM

if tank.contents then --#does tank.contents have a value
  --#do something with tank.contents
end

--#you can take this one step more too:
if tank then --#do I have a tank?
  if tank.contents then --#does tank have something in it?
	--#do something with tank.contents
  end
end

this works because in an if statement, nil returns false. If something is empty then it 'contains' nil
Edited on 01 October 2015 - 10:15 PM
Dragon53535 #3
Posted 02 October 2015 - 12:16 AM

if tank == nil then
print("OH HEY WE HAVE NOTHING IN THIS TANK")
end
Pretty sure i've solved this problem with you before dusty

Edit: Kinda ninja'd, although I was looking for code that I've written with dustmuz that covered this exact scenario…
Edited on 01 October 2015 - 10:17 PM
Dustmuz #4
Posted 02 October 2015 - 05:44 PM
This was solved using the example given by Lupus590 and Dragon's help via skype :D/>