Filtering values on a table?

So, I don’t quite know the method to do this, but I have a table based on ents.FindInSphere:()

for k, v in pairs( ents.FindByName(“smg0”)) do
smg0_targets = ents.FindInSphere( v:GetPos(), 500)
end

So this returns all the entities(player and a bunch of other junk) that I was wondering if I could somehow filter it into a new table using ents.FindByClass(“func_tracktrain”) on the old table to create a new table of entities that only contains func_tracktrain in the entities given in the last table by the sphere.
Thanks in advance.

[lua]
local trains =
for k, v in pairs( ents.FindByName(“smg0”)) do
for key, value in pairs( ents.FindInSphere( v:GetPos(), 500)) do
if value:GetClass() == “func_tracktrain” then
trains.Add(value)
end
end
end
[/lua]
This should do what you want, also the general questions thread (http://www.facepunch.com/showthread.php?t=1160598) is a much better place to ask these then in your own thread

The should be replaced with {}.

Btw i’m not entirely sure, but i don’t think you can do

[lua]
trains.Add(value),
[/lua]

i think you have to do

[lua]
table.insert(trains,value)
[/lua]

Erk. That’s not gonna work.

[lua]local targets, i = {}, 1
for k, v in pairs(ents.FindByName(“smg0”)) do
for x, y in pairs(ents.FindInSphere(v:GetPos(), 500)) do
if IsValid(y) and y:GetClass() == “func_tracktrain” then
targets* = y
i = i + 1
end
end
end[/lua]

You could use table.insert, but keeping track of the index yourself is dramatically more efficient.

thanks a ton man, this worked perfectly