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

Check if item in array help

Started by gr8pefish, 20 June 2014 - 09:24 PM
gr8pefish #1
Posted 20 June 2014 - 11:24 PM
Hi,

I have a relatively small question. I am using a touchscreen and trying to detect which page I am on so that I only visually update the current page. However, the page length is dynamic, so I want to loop through the total number of pages for each category and check if the current page is part of that category and then get the exact page. Is there any "fancy" or clean way to do what I have in mind?

Also, this whole code snippet is in a loop already, so I want a relatively 'efficient' way to do it (I don't want to loop through everything again to check what page/category it is if possible).

Here is the idea that doesn't work because the syntax of the elseif statements are incorrect:


if currPage == emptyPage then
	   --do stuff for this page
elseif
   for i = 1, #emptyJarsPages do
	  if t == emptyJarsPages[i] then
		  --Do stuff for that page
	  end
   end
elseif
   for i = 1, #fullJarsPages do
	  if t == fullJarsPages[i] then
		  --Do stuff for that page
	  end
   end
end

Essentially something that has the functionality of: elseif (currPage is in the array fullJarsPage) then

Thanks!
Lyqyd #2
Posted 21 June 2014 - 12:26 AM
Would it work to add a simple Boolean variable and update it every time you switch pages?


local currentPageIsFullPage = true

if currentPageIsFullPage then
  --# do stuff here
else
  --# it ain't full, do other stuff
end
gr8pefish #3
Posted 21 June 2014 - 12:36 AM
Hey Lyqyd,

Yeah, that would work. The only reason I avoided doing so is because I have to do it for 4 different categories, but it is a good idea, and the one I will probably go with.

Edit: Derp, I was thinking of the wrong array, looping through those to set up a couple booleans will be plenty fast. Thanks!
Edited on 20 June 2014 - 11:00 PM