I have a string that might be “<” or “>” and I’m not sure how to get it to be a conditional statement without using an if statement to check if “<” is the string and do the less than operation.
Is this possible to do without an if statement described above?
loadstring is way overkill for this bruh. You’re spinning up a whole lua compiler just to figure out a single character. On top of that, you might open yourself up to some bad vulnerabilities if you set LoadStringEnabled to true.
Just stick your if statement in a ModuleScript and never look at it again if it really bothers you.
That’s some creative code you got there! Unfortunately, I’d need to check for both greater and less than in the case of my code, but thanks for the cool solution!
Actually, my solution implies a more simple solution using only one conditional statement.
function check(one, operator, two)
return operator=="<" and one<two or operator==">" and one>two
end
--or if you only use it once
if operator=="<" and one<two or operator==">" and one>two then
--Statement is correct
end
Let me know if you have a different use case. Your follow-up seems to be different from your question.
I’ll just explain it with the content of what I’m making. I have a system where you can define group ranks as “>100” and the data assigned to that rank would apply to those whose ranks are greater than 100, for example. Alternatively you could have it “<100” and apply to people who have ranks less than 100.
It would seem that this solution is even better (and more readable lol) than your older one. This should apply to my situation, I imagine.
local ranks = {
[0] = "Guest",
[20] = "Member",
[100] = "Admin",
[255] = "Owner"
}
function getAll(group, input)
local t = {}
for rank, name in pairs(group) do
--Single line conditional to check if rank is within specified range
if input:sub(1,1)=="<" and rank<tonumber(input:sub(2)) or input:sub(1,1)==">" and rank>tonumber(input:sub(2)) or tonumber(input) == rank then
table.insert(t, name)
end
end
return t
end
table.foreach(getAll(ranks, "100"), print)
table.foreach(getAll(ranks, "<100"), print)
table.foreach(getAll(ranks, ">100"), print)
If you’d not want a single number to be a valid input, you can remove the last or bit from the statement.