users={[1]="user1", [2]="user2",}
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Compare all variable within a table
Started by jamd315, 07 February 2014 - 03:59 PMPosted 07 February 2014 - 04:59 PM
I want to easily compare all the variables in a table, prefferably with out a loop. Is there any easy way that I don't know about?
Posted 07 February 2014 - 05:05 PM
You can use the pairs() iterator, it's probably the best way for a beginner:
local hi = {"hello", "heya", "hi", "howdy"}
for key, value in pairs(hi) do
if value == "hi" then
print("Correct value.")
end
end
Posted 07 February 2014 - 05:08 PM
Ok, thanks for the quick reply :)/>
Posted 07 February 2014 - 05:10 PM
You are welcome. Glad I could help.
Posted 07 February 2014 - 06:18 PM
Without a loop:
local greets = {["hello"] = true, ["heya"] = true, ["hi"] = true, ["howdy"] = true}
if greets["hi"] then
print("Correct value.")
end
Posted 07 February 2014 - 06:29 PM
I want to easily compare all the variables in a table, prefferably with out a loop. Is there any easy way that I don't know about?users={[1]="user1", [2]="user2",}
Posted 07 February 2014 - 06:45 PM
Which is trivial to rewrite as:
users={["user1"] = true, ["user2"] = true}