Are there more ways to loop through a table?

yea I can tell you have very little knowledge when it comes to these things

dictionarys are tables and that’s a fact

also arrays are only 1D if the table goes 2D it becomes a matrix

Where in roblox does it say that?
Also, if it were true, why do the functions that require Tables or Dictionaries fail if you give them the other?

give me an example so I can explain

which part are you confused about?

Everybody forgets about this function:

table.foreach(Table, function(k,v) 
  print("key: ", k, "value: ", v);
end)

I find it very flavorful

Edit: oops I just saw someone else mentioned it :stuck_out_tongue:

1 Like

foreach and foreachi are so clean looking in my opinion, also I don’t mind if this was said before

In everything you said, show a screenshot with the link that Roblox says that, I have done it in the comparison. this is off topic lol

when did I ever say roblox said that, I’m just saying complete facts

if you want to keep talking then do it in pms/dms

If it is really true that Tables and Dictionaries are the same, there must be something said by them, not what you, me or another player say, right? The images that I put are taken from the wiki, those are real tests until they change it, in the end our discussion was about that.

all dictionaries are tables but not all tables are dictionaries

Yes, it’s true, but it’s not the same for functions or things like that and index and key is the same, that’s my point.

give me one example how any of my statements are wrong when looking at functions

also take this to pms/dms please

Okay, this conversation is so fun and interesting.

1 Like

I made a post awhile ago describing every type of loop that is available in Lua, feel free to have a look:

1 Like

You should do more research.

2 Likes

There’s a lot of potential misinformation or statements with questionable validity here, so I want to set the record straight. I’ve split up the post into sections since the original post was a mess, but I want it to be clear that the “misinformation” is not strictly false rather than misconceptions, and that the “questionable” information is more style/stuff I think leans into misconceptions rather than something being false per se.

A quick summary of my post would be:

Dictionaries and arrays are specific types of tables based on what keys they have. Mixed tables are tables with both kinds of keys. Dictionaries and the non-array portion of mixed tables do not inherently have order, and will vary based on when you run it. The order in which you define the elements does not matter if you are specifying the key, and keys and indices are the same thing. for i, v in pairs(Table) do and for i, v in next, Table, nil do are also the same thing. Avoid using the abnormal methods of looping through tables like numeric for loops, while loops, repeat loops, or foreach(i), with the exception of numeric for in very specific circumstances.


"Misinformation"

pairs, ipairs, next, and... numeric for loops?

I think it’s important to understand what each function is actually doing here. These are not the same and treating them all as “ways to loop through a table”, while technically correct, is not really the point. for returns... in func, args... do is roughly the syntax for what we call a “generic for loop”, as opposed to a numeric for loop. It runs the function “func” with the initial args of args... (can be multiple) and continues calling it with the values it returns until it returns nil. Each time it returns, those values are given as returns... (can be multiple) and the loop is run for one iteration with the returned values under those names. The names can be anything, too, since the returned values are assigned based on order. for i, v in pairs{} is fine, as is for index, value in pairs{}, and so on. More information is available in PiL.

This is relevant here because, while for i, v in pairs(Table) do is the “typical” form of a generic for loop, it is actually not the most “simplified” form. First pairs is called once, and then whatever is returned is used to repeatedly call for each iteration of the loop.* In pairs’ case, it returns a function which we refer to as next, which takes a table and a key and then finds the ‘next’ key (and associated value) in the table. pairs also returns the table passed and nil (for the initial index). Therefore, one could write pairs’ definition as this:

function pairs(t) -- it's a global, so not `local function`
	return next, t, nil
end

*: Technically this is different in Luau due to optimizations, but the functionality is the same.

I’m only bringing this up because you listed pairs(Table) and next, Table as separate definitions. In reality, it’s the same functionality; it will loop through a table, its array indices, and its dictionary indices, in no particular order. If you want a specific starting index, you can do for i, v in next, Table, start do and it’ll start right after start. Tables like pairs here are sometimes called “iterator factories”, as they are functions that exist to produce an “iterator function” like next. ipairs is another example of this, but it exists only to loop through the array part of a table (starting at 1, incrementing by 1, and stopping at nil).

The final one listed in the original post isn’t even really a way to loop through a table; it’s just a numeric for loop. In this case, it goes for a number of iterations equal to the number of elements in the array portion of the table. You can use that iterator as an index to the table to get all elements in the “array portion” of the table, but I don’t really think there’s any good reason to do that when readily made functions like ipairs exist.


Indexes, keys, and ordering

In the last section, you might have noticed that I called them “array indices” and “dictionary indices”. Normally you will see dictionary keys referred to as keys rather than indices (or indexes), but they are the same thing. The technical definitions are pretty much the same, too, as given by Google:

index (noun) (Computing): a set of items each of which specifies one of the records of a file and contains information about its address.

key (noun) (Computing): a field in a record which is used to identify that record uniquely.

Both are something that refers to something else within a record, and they refer to only one “something else”. The words might be used slightly differently in places outside Roblox/Lua, but they are one and the same in Lua due to arrays and dictionaries being the same thing, which I’ll go into more detail about in a bit. First, though, I want to talk about this:

There is no defined order for a dictionary. Order only exists when the keys/indexes (same thing!) are ordered—which is to say, they are arrays. Don’t take my word for this, though! This can be easily proven with a quick test:

Reordering based on the same code (gif)

qUIVWEbn6k


Arrays, dictionaries, and tables

Arrays and dictionaries are both colloquial terms used to refer to specific types of tables. Lua only has one datatype, the table, but arrays tend to be those with ordered numeric indexes starting at one, and dictionaries tend to be those with unordered numeric keys (such as Players’ UserIds) or non-numeric keys (usually strings). Mixed tables refers to tables with an “array portion” (ordered numeric indexes) and a “dictionary portion” (hopefully non-numeric; I don’t know that anyone is mixing arrays with something like UserIds but if they do they should stop before morgan/builderman/another person with a low UserId joins).

The important bit here is that none of the terms besides table have a strict definition. Some people may think that unordered numeric indexes are arrays, or that arrays can start at any number. When the documentation uses these terms, it usually either refers to what definitions I gave, or, even better, it will specify. Either way, statements like these are incorrect:


"Questionable"

foreach and foreachi

These have been around for a while. I thought they were deprecated at one point, but it looks like that is not the case from the current state of the wiki. Regardless, I would personally recommend using loops instead.


while loops, repeat loops, and similar.

Honestly, this pretty much sums it up:

Don’t use these. Numeric for is fine for the specific cases of getting “x” elements from a table, or randomizing the order you get them in, but while loops and repeat loops shouldn’t be used for this. If you know it’s going to be run for exactly “x” iterations, regardless of whether “x” is a constant or something dynamic like length, you should always be using a for loop rather than one with a conditional. Also, if the goal is to randomize the order at which you get elements from a table, it’s probably better to shuffle it and then loop through normally than to randomly pull elements while removing every element from the table in the process. Not sure what Heartbeat has to do with loops, either:


There are lots of great resources out there that do a better job than I ever could explaining these topics, so here’s a couple:

https://developer.roblox.com/en-us/articles/Table

PiL, but ignore the bits with the IO library

https://www.lua.org/pil/4.3.5.html
https://www.lua.org/pil/2.5.html
https://www.lua.org/pil/3.6.html
https://www.lua.org/pil/11.html

PiL chapter 11 has several sections; go through with the arrows.


Resources elsewhere on this forum

Tables - A better understanding & perspective on how to use them

Basic Lua Tables 101

6 Likes

I’ll make this the solution so everyone can see this info, only two things I want to mention related to the misinfo section

first thing

I already knew they were they same and I never listed them as different things! My point in this post was that they were different ways to write the code.

second thing

yes you are right but my main thought was that you could do Table[i], allowing you to get every value of a numbered table(unless it’s a matrix).

anyways thanks a lot for this post, always good to hear from other people :grin: