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

Or Statement inside "in pairs"

Started by bubblesayoyo, 10 February 2014 - 05:02 PM
bubblesayoyo #1
Posted 10 February 2014 - 06:02 PM
So I'm writing some code to 'count' my tanks and display onto a screen and I'm pretty close to finished, however I've run into a bit of a snag. The tanks are all connected through a wired modem via the top, and the monitor is connected to the left. The code in question is this:

for k,v in pairs(tempList) do
if v~= "top" then
  table.insert(tankList, v)
end
end

This returns:
rcsteeltank_0
rcsteeltank_1
etc…

This works perfectly, however, when I 'add' the monitor, I need the above tankList to ignore both 'top' and 'left.' Naturally I make it:

for k,v in pairs(tempList) do
if (v~= "top") or (v~= "left") then
  table.insert(tankList, v)
end
end

This returns:
top
rcsteeltank_0
etc…
left

So my question is, can you not use the or statement inside the 'in pairs' statement? Thanks in advance for any help that you can give! The total code is listed below for any reference needed.


--mon = peripheral.wrap("left")
--mon.clear()
--mon.setCursorPos(1,1)
tempList = peripheral.getNames()
num = (#tempList)
tankList = {}
varNum = {}
for k,v in pairs(tempList) do
if k~= "top" then
  table.insert(tankList, v)
end
end
for x,y in pairs(tankList) do
table.insert(varNum, peripheral.wrap(tostring(y)))
end
mPos = 1
for w, b in pairs(varNum) do
for s, e in pairs(b.getTankInfo("top")) do
  for a,n in pairs(e) do
   if a == ("rawName") then
	print(n)
	--mon.setCursorPos(1,mPos)
	--mon.write(n)
	--mPos = mPos+1
   end
   if a == ("amount") then
	print(n)
	--mon.setCursorPos(1,mPos)
	--mon.write(n)
	--mPos = mPos+2
   end
  end
end
end
CometWolf #2
Posted 10 February 2014 - 06:13 PM
You're using the or statement incorrectly. The if block will be executed regardless, as what you're doing is checking if v is "top", if it is top, it will check if it's "left", which it obviously isn't.
What you want instead is an "and". This way both conditions must be true for the block to execute. Also, the parentheses are unessacary.

for k,v in pairs(tempList) do
  if v~= "top" and v~= "left" then
	table.insert(tankList, v)
  end
end
Edited on 10 February 2014 - 05:14 PM
bubblesayoyo #3
Posted 10 February 2014 - 06:23 PM
The parentheses were just so I could easily cut and paste while playing around/testing. Thanks for the reply I will try this! Another quick question, I saw somewhere that if you don't need the first variables in the 'in pairs' you can just make it like this..


for ..,v in pairs(temList) do
  if v~= "top" then
    table.insert(tankList,v)
  end
end

That would work right? Again, thanks for the help, trying to teach myself something new and easier to just ask sometimes that search!
CometWolf #4
Posted 10 February 2014 - 06:31 PM
That would probably error, as ".." is concatenation. You can however name the variables whatever you like. A normal convention for variables you don't intend to use is to start them with an underscore.

for _k,v in pairs(temList) do
  if v~= "top" then
	table.insert(tankList,v)
  end
end
Edited on 10 February 2014 - 05:31 PM
bubblesayoyo #5
Posted 10 February 2014 - 06:34 PM
Thats what it was, I know I read it somewhere and just forgot. Thanks again for the help, much appreciated!