• Comparing multiple values
    18 replies, posted
Is it possible to compare, say 10 values and check if they're all the same? I'm working on a autokick for spawning 10 of the same prop in a row.
[lua] if ( x == y == z ) then -- etc -- do stuff end [/lua]
Thank you kindly.
[QUOTE=dr.dray_7;40695763][lua] if ( x == y == z ) then -- etc -- do stuff end [/lua][/QUOTE] It doesn't work like that. Everything after the first comparison is compared against a boolean value, e.g. (1 == 1 == 1) turns into (true == 1) which obviously returns false. This is how it should be: [lua] if(x == y && x == z && y == z) then -- etc -- do stuff end [/lua] Since that'll get confusing with ten variables, just use a table instead: [lua] local t = { -- Your 10 values "a", "a", "a", "a", "a", "b", "a", "a", "a", "a" } local function CompareValues(t) for _,v in ipairs(t) do for _,v2 in ipairs(t) do if(v != v2) then return false end end end return true end if(CompareValues(t)) then -- do stuff end [/lua]
[QUOTE=Silverlan;40696662][lua]if(x == y && x == z && y == z) then -- etc -- do stuff end [/lua][/QUOTE] If x==y and x==z, why wouldn't you assume y==z?
that's just nitpicking, he was showing the concept and how it results in the need for tables for more complex versions
[QUOTE=Silverlan;40696662]It doesn't work like that. Everything after the first comparison is compared against a boolean value, e.g. (1 == 1 == 1) turns into (true == 1) which obviously returns false. This is how it should be: [lua] if(x == y && x == z && y == z) then -- etc -- do stuff end [/lua] Since that'll get confusing with ten variables, just use a table instead: [lua] local t = { -- Your 10 values "a", "a", "a", "a", "a", "b", "a", "a", "a", "a" } local function CompareValues(t) for _,v in ipairs(t) do for _,v2 in ipairs(t) do if(v != v2) then return false end end end return true end if(CompareValues(t)) then -- do stuff end [/lua][/QUOTE] Yeah, I noticed that sorry for the bad reply. I was really out of it yesterday, but his method is right.
[QUOTE=Silverlan;40696662]It doesn't work like that. Everything after the first comparison is compared against a boolean value, e.g. (1 == 1 == 1) turns into (true == 1) which obviously returns false. [lua] local t = { -- Your 10 values "a", "a", "a", "a", "a", "b", "a", "a", "a", "a" } local function CompareValues(t) for _,v in ipairs(t) do for _,v2 in ipairs(t) do if(v != v2) then return false end end end return true end if(CompareValues(t)) then -- do stuff end [/lua][/QUOTE] This isn't necessary. You don't have to do two loops. If just one is different from just one other, then you can return false. [LUA] local t = { -- Your 10 values "a", "a", "a", "a", "a", "b", "a", "a", "a", "a" } local function CompareValues(t) for _,v in ipairs(t) do if(v != t[1]) then --only need to check to see if they're all equal to the first one, because that means they are also equal to each other. Two things that are equal to the same thing are equal to each other. return false end end return true end if(CompareValues(t)) then -- do stuff end [/LUA]
Thanks for the code. Is there a way to check if the table is full?
[QUOTE=MuteTM;40709355]Thanks for the code. Is there a way to check if the table is full?[/QUOTE] What do you mean by full? lua tables have (almost) infinite space.
As in each variable (10) in the table has a value, or should I just count as I write values to the table?
[QUOTE=MuteTM;40709585]As in each variable (10) in the table has a value, or should I just count as I write values to the table?[/QUOTE] I'm not sure what you mean, if you want to check if a table has a certain index in a it, you can do : [lua] if ( tbl[ index ] ) then /* it has the index */ end [/lua] If you want to check if it has a certain amount of keys in it; [lua] if ( #tbl >= aCertainAmountOfKeys ) then /* It has a certain amount of keys */ end [/lua] Additionally, a nice check you can do to speed up table comparison is check if the lengths match, if they don't it saves looping; [lua] local function comp( t, t2 ) if ( #t != #t2 ) then return false; end for k, v in pairs( t ) do if ( !t2[ k ] || t2[ k ] != v ) then return false; end end return true; end [/lua]
Maybe he plans to remove entries from the table. Use table.remove(tab,index) to kill entries and have the table collapse the empty space, don't do table[index] = nil.
[QUOTE=Bletotum;40710569]Maybe he plans to remove entries from the table. Use table.remove(tab,index) to kill entries and have the table collapse the empty space, don't do table[index] = nil.[/QUOTE] That's exactly what I wanted to do. Thanks. What about counting the number of values in the table?
[QUOTE=MuteTM;40713973]That's exactly what I wanted to do. Thanks. What about counting the number of values in the table?[/QUOTE] You can count the length of a table with #. For example: [LUA] local tabl = {1,2,3,2,3,4,6} local tableLength = #tabl print(tableLength) [/LUA] That will print 7.
Keep in mind that # only counts sequential numeric keys. Gaps in the table are treated as the end, so #{ [1] = 1, [2] = 2, [3] = 3, [5] = 5 } is 3, and non-numeric keys are ignored, so #{ A = 12, B = 13, C = 14 } is 0.
[QUOTE=Kogitsune;40716708]Keep in mind that # only counts sequential numeric keys. Gaps in the table are treated as the end, so #{ [1] = 1, [2] = 2, [3] = 3, [5] = 5 } is 3, and non-numeric keys are ignored, so #{ A = 12, B = 13, C = 14 } is 0.[/QUOTE] Right. If you want to count a table otherwise, do something to the effect of [LUA] local tbl = {"some", text="goes", "here"} function TableLength(tbl) local len = 0 for k,v in pairs(tbl)do len = len+1 end return len end [/LUA]
[QUOTE=bobbleheadbob;40717238]Right. If you want to count a table otherwise, do something to the effect of[/QUOTE] You could just do table.Count(tbl), which already does that
Thanks guys. Here's the finished code. [code] function antiSpam(ply, mdl) -- Create Table if not ply.proptable then ply.proptable = {} end -- Insert newly spawned prop table.insert(ply.proptable, mdl) -- Shameless copypasta, credit to "bobbleheadbob" of the FacePunch forums. This is the table check. local function CompareValues(t) for _,v in ipairs(t) do if(v != t[1]) then --only need to check to see if they're all equal to the first one, because that means they are also equal to each other. Two things that are equal to the same thing are equal to each other. return false end end return true end -- Count table values, if 10 values exist, continue if table.Count(ply.proptable) >= 10 then -- Kick for propspam, otherwise simply empty the table. if(CompareValues(ply.proptable)) then table.Empty(ply.proptable) ULib.kickban(ply, tonumber("1440"), '24 hour ban - Propspam') else table.Empty(ply.proptable) end end end -- Hook the function. hook.Add("PlayerSpawnProp", "antiSpam", antiSpam) -- Reset timer timer.Create("cleartables", 15, 0, function() for k, v in pairs(player.GetAll()) do if not v.proptable then v.proptable = {} end table.Empty(v.proptable) end end) [/code] Edit: Added a reset timer, now bans using ULib.
Sorry, you need to Log In to post a reply to this thread.