Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do you get one or multiple items in a table without looping?

Asked by 5 years ago

Is there a way to do this, to pick out a specific item (or multiple items) from a table? Instead of doing this obnoxious loop

for Index, Var in next, Table do
    if Var == Something then
        --Code
    end
end

Which will cause lag spikes and use up lot's of memory because this line will be used a TON, especially when removing dead zombies from a table.

3 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Also known as an array.

local tbl = {"boo","noob","nene"}

local certain = tbl[2]
print(certain)

noob

An array calls a specific item within a table without looping it.

If you do not know it's index then, it will still work

local rand = math.random(1,100)
local rand2 = math.random(1,100)
local tbl = {}
table.Insert(tbl,rand)
table.Insert(tbl,rand2)
if tbl[2] == 5 then
    print("5 has been selected.")
end
0
without knowing its index fusionFSJAL 33 — 5y
0
wait nvm i have an idea fusionFSJAL 33 — 5y
0
Okay greatneil80 2647 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

It you are using an array then I would use a numeric loop it is much faster than using an iterator.

local function get(thing)
    for i=1, #tbl do
        if tbl[i] == thing then
            -- code
        end
    end
end

Better still I would just use a key, value pair. You can also have parts as keys.

local part1 = Instance.new("Part")
local part2 = Instance.new("Part")
local tbl = {}

tbl[part1] = "this is part 1"
tbl[part2] = "this is part 2"

print(tbl[part1])
tbl[part1] = nil
print(tbl[part2])
print(tbl[part1])

You can use the MicroProfiler to better understand what is causing lag in your game.

Hope this helps.

0
Wow - just tested it and `pairs` and `ipairs` are almost twice as slow as just using a `for` loop. Why does Lua even include the `ipairs` function if it's worse than the thing it's meant to replace? EDIT - a slightly more fair comparison puts the difference at around 20% extra, but the point stands. fredfishy 833 — 5y
0
pairs/ipairs/next are more for iterating over dictionaries and there are more steps involved. A numeric loop is direct access to that element with minimal overhead. User#5423 17 — 5y
Log in to vote
0
Answered by
fredfishy 833 Moderation Voter
5 years ago
Edited 5 years ago

What you want to do is index by a specific field. You might know this as a "dictionary" or a "hash table".

For example

players = {}
for i, v in pairs(game:GetService("Players"):GetPlayers()) do
    players[v.Name] = v
end

Now if we want to get a player from their name, instead of doing this...

targetName = "Bob"
player = nil
for i, v in pairs(game:GetService("Players"):GetPlayers()) do
    if v.Name == targetName then
        player = v
        break
    end
end

...which is O(n), we can do this...

player = players[targetName]

...which is a constant time look up, regardless of how many elements are in your data structure.

It's not a great example but the idea is always the same. I'd make it more specific but you haven't given a lot to go on.

Answer this question