Turn "<" or ">" into a real conditional statement

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?

I mean you could use loadstring to just concatenate in the operator but that sounds like overkill.

I would recommend just using an if statement.

I’m not too caught up with the status of loadstring, so why would it be overkill?

Because it’s too much work just for the sake of avoiding an if statement

loadstring(
[[if a ]] .. operator .. [[ b then
    -- your code
end]])()

Oh yeah. True. I’m just a little bothered by having code that basically just says:

if string == ">" then
	if something > other then
		--stuff
	end
elseif string == "<" then
	if something < other then
		--stuff
	end
end

You should only really need to do this once with an elseif check

if something > other then
	--stuff
elseif something < other then
	--stuff
else
-- also handle a case where it could be equal.
end

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.

1 Like

That code doesn’t check if the given string is a conditional operator though…

If one conditional is fine (which I assume it is based on your chosen answer) and you want to avoid loadstring, you can do

if (operator:byte()/2-30)==0==(something<other) and (operator:byte()/2-30)==1==(something>other) then
    --
end

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!

What do you mean? It works for all operators.

function check2(one, operator, two)
  return (operator:byte()/2-30)==0==(one<two) and (operator:byte()/2-30)==1==(one>two)
end

print(check2(2, "<", 2)==false) 
print(check2(2, "<", 3)==true) 
print(check2(2, "<", 1)==false) 
print(check2(2, ">", 2)==false) 
print(check2(2, ">", 3)==false) 
print(check2(2, ">", 1)==true)

print(check2(2, "=", 2)==true) 
print(check2(2, "=", 3)==false) 
print(check2(2, "=", 1)==false) 
print(check2(2, "a", 3)==false) 
print(check2(2, "?", 1)==false)

All tests return true, as wanted.

Oh, apologies. You mentioned in your post “If one conditional is fine” so I assumed that it would work for only one check. Thanks for clarifying. :sweat_smile:

Judging by the implementation of your function I think that’ll work for what I’m planning, so I’m going to switch to your answer.

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.

Something like this, I’d 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.

Close. This is what the config for my system looks like:

		[4361197] = { --Group id
			["255"] = { --Exact rank
				--stuff
			},
			[">99"] = { --Ranks over 99
				--stuff
			},
			["<100"] = { --Ranks under 100
				--stuff
			}
		}

I’ve got the system working already with your newest solution (thanks for that btw).