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

[Question] Involves variables, not sure about a title..

Started by 1vannn, 07 June 2013 - 07:10 PM
1vannn #1
Posted 07 June 2013 - 09:10 PM
I'm working on a somewhat-large script that sends information into a database, etc. but I am making a login system involving multiple users, so would be code be useable??

users = {"user1","user2"}
if input ~= users then
print("Your not allowed to do this")
end
Engineer #2
Posted 07 June 2013 - 09:16 PM
You would need to use a pairs loop:

local t = {
  'hi!',
  'hey!'
}

for key, value in pairs(t) do
   print(key..':'..value)
end
--> 1:hi
--> 2:hey!

so a function would be:

local checkFor = function(tablename, user)
   for k, v in pairs(tablename) do
     if v == user then
        return true
     end
   end
  return false
end
theoriginalbit #3
Posted 07 June 2013 - 10:49 PM
Or alternatively another option is to define your table like this

local users = {
  ["user1"] = true,
  ["some user"] = true,
  ["theoriginalbit"] = true,
  --# etc, etc, etc.
}
Then it would allow you to do this

if users[ input ] then
  print "success"
else
 print "failed"
end
Much cleaner code, and no need for `for` loops. Meaning that it runs more efficiently and there is no chance for a yield error if the table is very large.
1vannn #4
Posted 08 June 2013 - 04:18 PM
Why can't life be simple. Thanks guys for you're help. :)/>