Hello, in short, my real problem is that I'm at that level of scripting where you know how to do something, but only one way.
for i, link in pairs(links) do
if link.Switches ~= nil then
link.CancelledLinks = {}
for j, switch in pairs(link.Switches) do
for m, otherLink in pairs(links) do
if otherLink ~= link and otherLink.Switches ~= nil then
for b, otherSwitch in pairs(otherLink.Switches) do
if otherSwitch == switch then
table.insert(link.CancelledLinks, otherLink)
end
end
end
end
end
end
end
The purpose of this massively-layered loop is to determine what other values of the same table are "cancelled out" by each value. It does this by seeing if they have the one or some of the same switches in their "Switches" table. (This is for train tracks if you're curious.) What would you do instead of this messy code?

6 comments
Have one huge table perhaps?
ur table must be deeper than mattscys philosophical mind
An alternative to using "in pairs on top of in pairs" would be using "in ipairs on top of ipairs."
that makes sense, since the links table has numeric indices. Changed it, but there aren't fewer lines
Sometimes you just end up with a lot of nested for loops. Looking at that gives me a headache, but if... jesus that's a lot... anyways, adding another table may help a bit, but I seriously don't understand the whole thing. I reccomend using comments to make it easier to read.
I recommend using a table with indices that represent what link the link is. That way, when you loop through all of the links, you can quickly index a table to see if a link is nil or not. I don't quite understand what is going on in this script, but I do know that a good solution would be a table with representative indices.