I want to use this keyword almost as if I had a table of strings to compare throughout a table similar to the in statement in gs2
[QUOTE]local bla = {"one", "two", "three"}
local word = "one"
if word in bla then
// successfully compared word to table "bla"
end[/QUOTE]
is this possible?
A for loop would be good. Not really how you did it but:
[lua]
table = {}
table[1] = "one"
table[2] = "two"
table[3] = "three"
word = "one"
for 1,#table do
if table[i] == word then
//if it is a success
end
[/lua]
Untested
I don't think so, but you can use
[lua]
if table.HasValue(bla,word) then
//success
end
[/lua]
Or what I would do is
[lua]
bla={}
bla["one"]=1 --or true, or something.
bla["two"]=2
bla["three"]=3
word="one"
if bla[word]!=nil then
//successe
end
[/lua]
edit: Something like toaster said should work but I would do a break once word is found.
[QUOTE=ArmageddonScr;32700844]I don't think so, but you can use
[lua]
if table.HasValue(bla,word) then
//success
end
[/lua]
Or what I would do is
[lua]
bla={}
bla["one"]=1 --or true, or something.
bla["two"]=2
bla["three"]=3
word="one"
if bla[word]!=nil then
//successe
end
[/lua][/QUOTE]
that's a really really goosey way of doing things.
Why? What would you do?
[QUOTE=ArmageddonScr;32700844]
Or what I would do is
[lua]
bla={}
bla["one"]=1 --or true, or something.
bla["two"]=2
bla["three"]=3
word="one"
if bla[word]!=nil then
//successe
end
[/lua][/QUOTE]
This is the best way of doing it.
[QUOTE=Divran;32701086]This is the best way of doing it.[/QUOTE]
[lua]
if ( bla[word] ) then
-- l
end
if ( !bla[word] ) then return end;
[/lua]
[QUOTE=LauScript;32701146][lua]
if ( bla[word] ) then
-- l
end
if ( !bla[word] ) then return end;
[/lua][/QUOTE]
Suppose bla["something"] is 0. The first check is false and the script fails (EDIT: Another logic error in the second check). Besides, what is the second check for?
The second check is a different example
i'm just trolling. uknohowitiz butnrly
[LUA]
local tbl = {one=true, two=true, three=true}
local word = "one"
if(tbl[word])then
--1
end
[/LUA]
Actually this is a better way. Initializing the table with the already known static values reduces time to access the table to insert new values, using true instead of numbers uses less memory, and checking a value that is either true or nil is faster than checking if not nil.
[url]http://wiki.garrysmod.com/?title=Table.HasValue[/url] was what I was looking for, thanks!
Sorry, you need to Log In to post a reply to this thread.