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

Compare all variable within a table

Started by jamd315, 07 February 2014 - 03:59 PM
jamd315 #1
Posted 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?

users={[1]="user1", [2]="user2",}
awsmazinggenius #2
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
jamd315 #3
Posted 07 February 2014 - 05:08 PM
Ok, thanks for the quick reply :)/>
awsmazinggenius #4
Posted 07 February 2014 - 05:10 PM
You are welcome. Glad I could help.
Bomb Bloke #5
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
awsmazinggenius #6
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",}
Bomb Bloke #7
Posted 07 February 2014 - 06:45 PM
Which is trivial to rewrite as:

users={["user1"] = true, ["user2"] = true}