|
| 20 May 2014 04:07 AM |
So, I was wondering if I could scan two tables like this, would it work? :
Table1 = {"Lol"} Table2 = {"Haha"}
function tableContains(Table1, value) for _, v in pairs(Table1) do if v == value then return true end end return false end
function tableContains(Table2, value) for _, v in pairs(Table2) do if v == value then return true end end return false end |
|
|
| Report Abuse |
|
|
|
| 20 May 2014 04:08 AM |
| Also are there any more simple and short methods of doing this? |
|
|
| Report Abuse |
|
|
jasondee1
|
  |
| Joined: 26 Jul 2008 |
| Total Posts: 8002 |
|
|
| 20 May 2014 04:18 AM |
| You'd have to name the second function something different, |
|
|
| Report Abuse |
|
|
|
| 20 May 2014 08:12 AM |
Can't you just do
function TableContains(T, V) if T[V] then return true else return false end end |
|
|
| Report Abuse |
|
|
Ceoh
|
  |
| Joined: 17 Nov 2013 |
| Total Posts: 152 |
|
|
| 20 May 2014 08:33 AM |
@warspyking
No he can't, because that won't work. T[V]? lol for someone who promotes the use of tables even in situations where it's clearly inefficient and irrational, you should learn more about how to use it.
Tables 101 from the wiki: "You put the key in square brackets ([]) after the table that is to be looked in." You put the Value in the square brackets -_-
His function: function tableContains(Table1, value) --note the second variable, value
Your function: function TableContains(T, V) if T[V] then return true --What are you doing here? Trying to index a value I see...
GG
|
|
|
| Report Abuse |
|
|
|
| 20 May 2014 08:38 AM |
Okay, good to know XD
I thought I saw someone do that before...
function Table.Destroy(T, Va) for _,v in ipairs(T) do if T[v] == Va then table.remove(T, v) end end end
Works just like table.remove, except will let you put in the value instead of key. |
|
|
| Report Abuse |
|
|
|
| 20 May 2014 09:39 AM |
Ohh, ok so if I change the names of the functions to something different, it should work?
function tableContains1(Table1, value) for _, v in pairs(Table1) do if v == value then return true end end return false end
function tableContains2(Table2, value) for _, v in pairs(Table2) do if v == value then return true end end return false end |
|
|
| Report Abuse |
|
|
|
| 20 May 2014 09:44 AM |
Why do you need a seperate table?!?!?
Use the same function for both tables.
function tableContains(Table, value) for _, v in pairs(Table) do if v == value then return true end end return false end
tableContain(TableNameHere, Value) |
|
|
| Report Abuse |
|
|
|
| 20 May 2014 09:47 AM |
| It's just because the stuff in table 1 will have different privileges then table 2 so I want to make sure that lets say table 1 will have extra stuff. |
|
|
| Report Abuse |
|
|
|
| 20 May 2014 09:48 AM |
| Would the script that I changed the names of the functions work properly? |
|
|
| Report Abuse |
|
|
| |
|
|
| 20 May 2014 09:54 AM |
| You only Need one function. You can then use it on both. If you make two, they would both do the same thing. |
|
|
| Report Abuse |
|
|
|
| 20 May 2014 09:54 AM |
| Also aren't there any shorter ways of doing this? |
|
|
| Report Abuse |
|
|
|
| 20 May 2014 09:55 AM |
| Ohh ok, but when it says: function tableContains(Table2, value) Does Table2 stand for the table called Table2? |
|
|
| Report Abuse |
|
|
|
| 20 May 2014 10:01 AM |
Table2 was an argument. What you add there when you call it is what it will use.
So, I was wondering if I could scan two tables like this, would it work? :
function tableContains(Table2, value) --This line varuablize s what you add later for _, v in pairs(Table2) do if v == value then return true end end return false end
Can be used on Table1
tableContains(Table1, "Hi") --This line determines the tables. |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 20 May 2014 10:04 AM |
I would do it like this local tab = {} function tab.find(Table, x) for Index, Value in ipairs (Table) do if Value == x then return Index end end return nil end
local a = {"Hi", "Hey", "Hola", "Hello"} print(tab.find(a, "Hi")) -->1 print(tab.find(a, "Hola")) -->3 print(tab.find(a, "Im not in your table.")) -->nil
|
|
|
| Report Abuse |
|
|
|
| 20 May 2014 09:35 PM |
Ohhh, so i get it now, so with only one of the functions (tableContains), I can do something like this to see if the player is on both tables?
if msg == "randomlol" and tableContains(Table1, player.Name) or tableContains(Table2, player.Name) then |
|
|
| Report Abuse |
|
|
|
| 20 May 2014 09:38 PM |
function tableContains(t, value) for _, v in pairs(t) do if v == value then return true end end return false end
if msg == "randomlol" and tableContains(Table1, player.Name) or tableContains(Table2, player.Name) then
This is all together |
|
|
| Report Abuse |
|
|
|
| 20 May 2014 09:41 PM |
You could just do...
function tableContains(Value) for _,o in pairs({Table1,Table2}) for _,n in pairs(n) if o == Value then return true end end end end
local Table1 = {} local Table2 = {}
if tableContains("Bob") then Explode() end |
|
|
| Report Abuse |
|
|
|
| 20 May 2014 09:42 PM |
| Ok but would the script I posted still work with Table1 and Table2? |
|
|
| Report Abuse |
|
|
|
| 20 May 2014 09:44 PM |
| It should, why don't you test it? |
|
|
| Report Abuse |
|
|
|
| 20 May 2014 09:46 PM |
| All right thanks, I just needed it like that because I wanted to create restriction example: Table1 can say reset but Table 2 can say reset and kick. |
|
|
| Report Abuse |
|
|