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

How To Avoid Elseif-Chains?

Started by Gorni, 24 February 2012 - 06:57 AM
Gorni #1
Posted 24 February 2012 - 07:57 AM
Hi again everyone. Im back with another question.
I'm still a noob in coding but im learning, so i have a lot of questions. :huh:/>/>

I got a part like this in my code:

--Storage filling, every pulse will have another color, not dedicated yet.
feedc = 0

function storage()
	 
    if feedc == 0 then
	 rs.setBundledOutput("back", colors.xxx)
	 sleep(0.5)
	 rs.setBundledOutput("back", 0)
	 sleep(0.25)
	 feedc = feedc + 1
    elseif feedc == 1 then 
	 rs.setBundledOutput("back", colors.xxx)
	 sleep(0.5)
	 rs.setBundledOutput("back", 0)
	 sleep(0.25)
	 feedc = feedc + 1
    elseif feedc == 2 then
	 rs.setBundledOutput("back", colors.xxx)
	 sleep(0.5)
	 rs.setBundledOutput("back", 0)
	 sleep(0.25)
	 feedc = feedc + 1
    elseif feedc == 3 then
	 rs.setBundledOutput("back", colors.xxx)
	 sleep(0.5)
	 rs.setBundledOutput("back", 0)
	 sleep(0.25)
	 feedc = feedc + 1
    elseif feedc == 4 then
	 rs.setBundledOutput("back", colors.xxx)
	 sleep(0.5)
	 rs.setBundledOutput("back", 0)
	 sleep(0.25)
	 feedc = feedc + 1
    elseif feedc == 5 then
	 rs.setBundledOutput("back", colors.xxx)
	 sleep(0.5)
	 rs.setBundledOutput("back", 0)
	 sleep(0.25)
	 feedc = feedc + 1
    elseif feedc == 6 then
	 rs.setBundledOutput("back", colors.xxx)
	 sleep(0.5)
	 rs.setBundledOutput("back", 0)
	 sleep(0.25)
	 feedc = feedc + 1
    elseif feedc == 7 then
	 rs.setBundledOutput("back", colors.xxx)
	 sleep(0.5)
	 rs.setBundledOutput("back", 0)
	 sleep(0.25)
	 feedc = 0
    else
   end
end
 

how can i write it different by avoiding long elseif chains with like something shorter? i want no loop, since it will get activated by another function. Is there a way to do a check for a variable and the depending on the number, doing another little code block?

Thx for your patience

- Gorni
Niarbius #2
Posted 24 February 2012 - 09:55 AM
I'm fairly new at Lua as well, but I have used other programming languages, so hopefully this works for you:


--Storage filling, every pulse will have another color, not dedicated yet.
feedColors = { colors.xxx, colors.xxx } -- put all your colors here
feedc = 1  -- Starting at 1 because the above array/table starts at 1 (I think)

feedcMax = feedColors.length() -- not sure if this function exists/is correct, but this should be the number of colors you have in feedColors
function storage()
    -- Set the bundled output to the corresponding color in the table
    rs.setBundledOutput("back", feedColors[feedc] )
    sleep(0.5)
    rs.setBundledOutput("back", 0)
    sleep(0.25)
    feedc = feedc + 1
    if feedc > feedcMax then
        feedc = 1
    end
end

That will reduce your IF statement to a one-liner, with no elseifs at all B)/>/>
Just put all 8 (or however many) colors you want in feedColors.

Edit: Hmmm, the indent doesn't seem to work for me. Oh well…. :huh:/>/>
Espen #3
Posted 24 February 2012 - 11:57 AM
The color-names (color.white and the like) represent numerical values based on the binary system.
If you look into the file "/rom/apis/colors", then you will see this at the top:

-- Colors
white = 1
orange = 2
magenta = 4
lightBlue = 8
yellow = 16
lime = 32
pink = 64
gray = 128
lightGray = 256
cyan = 512
purple = 1024
blue = 2048
brown = 4096
green = 8192
red = 16384
black = 32768
So if you start at e.g. yellow and want to get to lime, you just multiply yellow's value by 2 and get the value for lime (16 * 2 == 32).
That's the principle I used in the code below.
Note though that this only iterates through all 16 colors, it doesn't combine any of them. But I think that's all you wanted, right?
Also in your original code you started at 0, but that is not a color, but the signal to shut off all signals on a cable.

Here goes, haven't tested the code ingame yet, though:

--Storage filling, every pulse will have another color, not dedicated yet.
feedc = 1  -- colors.white
feedcMax = 32768	-- colors.black

function storage()
	-- Set the bundled output to the corresponding color in the table
	rs.setBundledOutput("back", feedc )
	sleep(0.5)
	rs.setBundledOutput("back", 0)
	sleep(0.25)
	feedc = feedc * 2
	if feedc > feedcMax then
		feedc = 1
	end
end
Gorni #4
Posted 24 February 2012 - 12:12 PM
now thats a cool way to go for it. :huh:/>/>
Im gonna test that right when i get home today, thanks a lot.
Gorni #5
Posted 24 February 2012 - 09:55 PM
Works wonderfully, thanks for this great idea!