Taking common elements from 2 vectors in Lua -
i have 2 vectors, lets v1 = (a,b,c,d,e)
, v2 = (e,h,t,b,w)
, want third vector containing common elements of v1
, v2
, in case v3 = (e,b)
i've seen asked in c++, cant see lua.
i assume when saying vectors, mean v1
, v2
both array-like tables, this:
local v1 = {1,2,4,8,16} local v2 = {2,3,4,5}
then can this:
local v3 = {} k1,v1 in pairs(v1) k2,v2 in pairs(v2) if v1 == v2 v3[#v3 + 1] = v1 end end end
Comments
Post a Comment