97 posts
Posted 17 April 2016 - 04:09 AM
I got a reactor with a ton of control rods. I'm wanting to make it check and see if all the control rods are equal to each other or throw and error up and shut down the reactor. the only part that has me stuck is an easy way to get them to compare to each other as I got 16 rods in it. I know about if x == y and y == z type thing but wondering if there's a simpler way as that would be one really big line of code.
7083 posts
Location
Tasmania (AU)
Posted 17 April 2016 - 05:04 AM
Let's say you've got all the reactors wrapped as peripherals within a table; you might do:
local firstRodLevel = reactor[1].getControlRodValueThingy()
for i = 2, #reactor do
if reactor[i].getControlRodValueThingy() ~= firstRodLevel then
error("Rods aren't the same!")
end
end
355 posts
Location
Germany
Posted 18 April 2016 - 01:19 AM
Likewise, you could also split it into multiple lines. with 15 comparisons still quite ugly in my opinion. Whitespace(multiple spaces, tabs….) including new lines are not evaluated by the interpreter. Just in case you or someone else looking for similar stuff doesn't know already. You know… to contribute to the completeness of the answer. I would prefer Bomb Blokes answer.
In some cases (I don't know about yours), you can also use arithmatic tricks, to check all rods at once. Like checking whether the math.min value is the same as math.max. Should be commented though, just so one knows, what this not so frequently seen comparison actually does.
1080 posts
Location
In the Matrix
Posted 18 April 2016 - 01:36 AM
Even if you check the math.min vs math.max, you have to loop through the table, it's more efficient to do BB's way since it does compare on the spot rather than after the fact. Plus BB's way allows for knowing exactly which rod is messed up.
355 posts
Location
Germany
Posted 18 April 2016 - 09:19 PM
Even if you check the math.min vs math.max, you have to loop through the table, it's more efficient to do BB's way since it does compare on the spot rather than after the fact. Plus BB's way allows for knowing exactly which rod is messed up.
Well, I was going small code size there, so everything actually does the same, but comparing all values of a table you can just do math.min(unpack(tbl)) == math.max(unpack(tbl))
Every method has it's up and downsides. This is just really compact, so I submitted it.
EDIT: Also, since the libraries are implemented "natively", the check of min against max might even be faster, even though you are practically looping through the whole table twice. (I can not proof this though, and it might not be the case. In CC specifically, I think it's quite unlikely, that it'll be faster than any instance of table compared to bb's answer.)
Edited on 18 April 2016 - 08:45 PM
7083 posts
Location
Tasmania (AU)
Posted 19 April 2016 - 12:34 AM
Well, I was going small code size there, so everything actually does the same, but comparing all values of a table you can just do math.min(unpack(tbl)) == math.max(unpack(tbl))
The problem is that you don't have a table filled with up-to-date rod-values unless you repetitively iterate through your peripherals and call their functions.
EDIT: Also, since the libraries are implemented "natively", the check of min against max might even be faster, even though you are practically looping through the whole table twice. (I can not proof this though, and it might not be the case. In CC specifically, I think it's quite unlikely, that it'll be faster than any instance of table compared to bb's answer.)
It'd likely depend on the number of values to check and whether they actually match. For a small set, slower (if only because you have to make function calls to do it). For a large enough set, it may eventually become faster, assuming all values usually match.