|
| 01 Jun 2012 04:10 AM |
I am experimenting with tables. How would you make it so that a line of code can check if there is a specific value in the table? e.g
myTable = {"Hi","Hello","World"}
How would you check if "Hello" is in the table, if it is in the table print("Yes").
Can somebody help? |
|
|
| Report Abuse |
|
|
Cheater
|
  |
| Joined: 29 Jun 2007 |
| Total Posts: 5258 |
|
|
| 01 Jun 2012 04:15 AM |
| Look how an admin script is built up. It also uses tables. Look at where it checks if the player is in that table. |
|
|
| Report Abuse |
|
|
oxcool1
|
  |
| Joined: 05 Nov 2009 |
| Total Posts: 15444 |
|
| |
|
| |
|
|
| 01 Jun 2012 11:37 PM |
Can you help me with this?
local players = {"Hello","Hi","World"}
function findPlayer(name) for _,v in pairs(players) do if v:sub(1) == name:sub(1) then return true end end end
print(findPlayer("W"))
^^^In that script I want to check if there is a word starting with W in the table.^^^
That does nothing but this works:
local players = {"Hello","Hi","World"}
function findPlayer(name) for _,v in pairs(players) do if v == name then return true end end end
print(findPlayer("World"))
Whats wrong? |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 01 Jun 2012 11:47 PM |
myTable = {"Hello","Hi","Bye"} function over_load(index) if myTable[index] == nil then return elseif myTable[index] == "Hi" then return "Yes" else over_load(index + 1) end end
over_load(1)
|
|
|
| Report Abuse |
|
|
|
| 01 Jun 2012 11:49 PM |
Thanks for your help miz, but I did find another easier way to do it.
local players = {"Hello","Hi","World"}
function findPlayer(name) for _,v in pairs(players) do if name == v:sub(1,name:len()) then return true end end end
print(findPlayer("Wo"))
That will check if the word 'Wo' is in any of the words in the table. You can also change 'Wo' to any word that is in a word in the table. |
|
|
| Report Abuse |
|
|
aboy5643
|
  |
| Joined: 08 Oct 2010 |
| Total Posts: 5458 |
|
|
| 02 Jun 2012 12:26 AM |
local players = {"Hello","Hi","World"} function findPlayer(name) for _,v in pairs(players) do return name == v:sub(1,name:len()) end end end print(findPlayer("Wo"))
Shortened for no reason other than to point out it does the same thing |
|
|
| Report Abuse |
|
|