Jump to content




Check If A String Is Somewhere In A Table?


  • You cannot reply to this topic
6 replies to this topic

#1 popdog15

  • Members
  • 82 posts

Posted 07 October 2013 - 09:25 PM

Hello, I'm making a program that requires a player to be "authorized" and I put all the authorized players within a table. After a player hits a player detector, I need to check if that player is within the table somewhere. Would it just be something like
if playername == authorizedplayers[] then
-- more important stuff later
? Or is there an easier way to do something with the same effect?

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 07 October 2013 - 09:48 PM

This is a very common question on these forums, and there are lots of answers to all of them, both from myself and others, as such, I'm not going do a big writeup here, why say something again when you can just point people to a place where you've already said it before.

Link #1
Link #2
Link #3

#3 0099

  • Members
  • 52 posts

Posted 07 October 2013 - 10:50 PM

Maybe you can create a table with player names as KEYS, and the values will be... For example, passwords. Then, you can check for this player like that:
if authorizedplayers[playername] == nil then print("Incorrect username") return end
if authorizedplayers[playername] ~= pass then print("Wrong password") return end


#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 07 October 2013 - 10:55 PM

View Post0099, on 07 October 2013 - 10:50 PM, said:

Maybe you can create a table with player names as KEYS, and the values will be... For example, passwords. Then, you can check for this player like that:
if authorizedplayers[playername] == nil then print("Incorrect username") return end
if authorizedplayers[playername] ~= pass then print("Wrong password") return end
Yes that method is the best way to do it, as detailed in all 3 of the links I provided in the reply above yours.
Also, a tip, use [code][/code] tags around code.
Lastly your == nil is not needed, doing if authorizedPlayers[playername] then will evaluate to true if there is a value, false if it is nil. Your second if statement can work without the first one too.

#5 Tjakka5

  • Members
  • 256 posts

Posted 09 October 2013 - 04:19 AM

local players = {"Tjakka5", "person1", "person2"}

local input = read

for i = 1, #players do
  if input == players[i] then
     --do stuff
  end
end

That should also work.


#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 October 2013 - 04:24 AM

View PostTjakka5, on 09 October 2013 - 04:19 AM, said:

local players = {"Tjakka5", "person1", "person2"}
local input = read
for i = 1, #players do
  if input == players[i] then
	 --do stuff
  end
end
That should also work.
Actually it wouldn't, you have a bug, but if you didn't have the bug, it would be very inefficient compared to a lookup table especially when there is a large amount of data in the table.

#7 AgentE382

  • Members
  • 119 posts

Posted 09 October 2013 - 07:24 AM

lol, I love how bit's trying to keep himself from explaining everything over again. You would have to change your code to:
local players = {"Tjakka5", "person1", "person2"}

local input = read()    -- forgot parentheses

for i = 1, #players do
  if input == players[i] then
     --do stuff
  end
end

He's right. It would still be more efficient to do:
local players = {Tjakka5 = true, person1 = true, person2 = true}

if players[read()] then
     --do stuff
end






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users