# Are there more ways to loop through a table?

``````for i, v in pairs(Table) do

end

for i, v in ipairs(Table) do

end

for i, v in next, Table do

end

for i = 1, #Table, 1 do

end
``````

are there more ways to loop through a table, this isn’t something I need to know. I am just really curious about this, that’s all

2 Likes

If it is with numeric index

``````local Table = {}
local Current = 1
while true do
if Current == #Table then				break				end

print(Current, Table[Current])
Current += 1
end
``````

and

``````local Table = {}
local Current = 1

repeat
print(Current, Table[Current])
Current += 1
until Current == #Table
``````

Why can this be used instead of for? I dont know
Both cases can be used for when you will only use X amount from a table, I don’t know, you want to select 20 objects at random.

1 Like

oh yea you could always do that

I’ll probably never use it anyways but at least I know it exists, thanks

Randomized iteration of array-style table.

``````local list = {"A", "B", "C", "D", "E", "F"} -- Some table

while #list > 0 do
local elem = table.remove(list, math.random(1, #list))
print(elem)
end
--NOTE: this method will not catch any (key, value) paired elements
--Also, after the while loop the table will be empty
``````
2 Likes

Yup, there are better options, this is also using functions like RunService.Heartbeat.

1 Like

oh trust me ik there are better options

1 Like

interesting didn’t think about removing the values in this way, don’t know where this could be useful but interesting indeed

There’s also a couple iterator functions that I think were added a while back. Definitely weren’t available (or at least documented in the wiki) when I was first learning:

``````local list = {"A", "B", "C", "D", "E", "F"} -- Some table

function doPrint(i, v)
print(i, v)
end

print("--foreach--")
table.foreach(list, doPrint)
print("--foreachi--")
table.foreachi(list, doPrint)
``````
1 Like

are these still a thing in luau?

``````local list = {"A", "B", "C", "D", "E", "F"} -- Some table

while #list > 0 do
local i = math.random(1, #list)
local v = list[i]
table.remove(list, i)
print(i, v)
end
``````

you can get the index and value
but not the key

Yes, by (key, value) paired elements I meant tables that had structures similar to:

``````mixed = {
"A",
"B",
"C" = {1,2,3,4}
}
``````

Technically string element “A” is at index (or key) `1` (`mixed[1]`); the same goes for string element “B” (`mixed[2]`), however that while loop would have no idea about the existence of key “C” since it exists at `mixed["C"]` – and the ‘#’ operator does not include non-array indexed elements in its count.

1 Like

This is what I think of when I hear index, key, and value

``````local Table = {
[3] = 3, -- index = 1, key = 3, value = 3
[2] = 4, -- index = 2, key = 2, value = 4
[1] = true -- index = 3, key = 1, value = true
}
``````

99% sure this is completely right

Not really, it’s like this:

``````local Table = {
[3] = 3, -- index = 3, key = 3, value = 3
[2] = 4, -- index = 2, key = 2, value = 4
[1] = true -- index = 1, key = 1, value = true
}
``````

since Studio will always order it.
If there is Index that is not a number, the table will be called Dictionary and index will be called key, although there are many (including myself) that use key for both.

1 Like

no this is completely false

the key is the name for the value and the index is simply the order

the index is always a number

Nope, I don’t say it, the output says it

no the 3 key value has an index of 1
roblox will auto order the table to make the index into 3 due to the key being a number

so simply the indexes switched and it was initially the index I said and then turned into the index you said

Technically key and index are the same but roblox says key for dictionaries and index for tables.

anyway, if you assign a key or an index to the object in a table, roblox will sort it out when it should return it.

2 Likes

first things first, key and index are not “technically” the same
second, dictionaries are tables

trust me ik what I’m talking about here

No, they are not for functions or things like that, why do you think some functions say Dictionary or Table?

array is used if it refers to both.