in pairs in pairs in pairs in pairs
For each script, I will use the same table.
local t = {"Hello", 123, true, [workspace] = false , "Hello again", ["Five"] = 5, nil, "Hello a third time..."}
There are arrays
and dictionaries
. An array is just a simple list of items numbered in order by when they appear in the table. A dictionary is slightly more advanced in that it stores not only the value, but the key can be changed as well.
local array = {1, true, Color3.new(), workspace.Part} --[[Items in this list are numbered. Lua uses 1-based indexing for arrays, meaning that arrays start from 1. Other coding languages you may be familiar with may start their lists from 0.]] local dictionary = {[1] = 1, ["A string!"] = false, [Color3.new()] = workspace.Part}
What does that mean? Well, let's say that we want to get a value from a table. How do we get a value? array[2]
is how we'd do it. You'd replace "array" with the name of the table and replace 2 with what item you want. What's inside of the square brackets is called the key
which allows you to get specific things from your table. In this example, 2 would give us true
because the 2nd item in the table, named array, is "true".
For a table, we'd call a value like this: dictionary["A string!"]
. "dictionary" is the name of the table and "A string!"
is a key that we've set earlier in the above code. In this example, we'd get "false".
Let's get into learning about our stateless iterators
!
Let's talk about what all of these functions do. They help us iterate through tables to try to get all of their values. So let's use table t
and make an example for loop
real quick...
local t = {"Hello", 123, true, [workspace] = false , "Hello again", ["Five"] = 5, nil, "Hello a third time..."} --Pretend this table is in all of the future examples for a,b in pairs(t) do print(a,b) end
The first variable a
gives us the key and b
gives us the value. This code will print in the output the key first and then the value. pairs
will iterate through the entire table and give us every single key and value in the table (as long as the value is not nil
or the value exists).
1 Hello 2 123 3 true 4 Hello again 6 Hello a third time... Five 5 Workspace false
As you can see, every value that is NOT nil
is printed. The keys and values are printed, even the keys that have been modified to say things like workspace
or "Five"
.
ipairs
is exactly like pairs, but with a few extra rules. ipairs will not give you the value if the key for that value is not an integer
.
for a,b in ipairs(t) do print(a,b) end
1 Hello 2 123 3 true 4 Hello again
ipairs will also break the loop when it hits a key whose number is nil. In this case, the ipairs is ignoring [workspace] = false
and ["Five"] = 5
. It counts "Hello again"
as the 4th item in the table. The loop stops after the loop reaches a value of nil
. "Hello a third time..."
would normally print, but because there was a nil value right before, it will not print.
next
is a primitive lua function.
for a,b in next,t do print(a,b) end
Same output as pairs
.
print(next(t)) --will print the first key and number print(next(t,nil)) --Does the same as the above line of code. --next starts like so: nil, 1, 2, 3... as if it's counting starting from 0. print(next(t,1)) --meaning that this will print the 2nd key and 2nd value.
I don't know if there is a name for this method or maybe I just can't recall it, but there is always this method:
for a = 1,#t do print(a, t[a]) --"t[a]" is the same as "b" in the other scripts. end
1 Hello 2 123 3 true 4 Hello again 5 nil 6 Hello a third time...
What you will notice is that the output is the same as ipairs
, but the loop will keep going even when it reaches a nil value. This is useful if you want to use ipairs
over pairs
, because the table is a mix of an array and a dictionary, but there are the occasional nil
values in the middle of the table.
It's pretty simple, ipairs is mostly using in module scripts, it works just like in pairs but the thing is it stops if any nil value is passed, it's used in module scripts to prevent errors, for example
-- An example Local test = {"Hi","Get Noob", nil,"Me noob"} for i,v in pairs(test) do print(v) end
Then the output will be
Hi Get Noob Me noob nil
For ipairs
Local test = {"Hi","Get Noob", nil,"Me noob"} for i,v in ipairs(test) do print(v) end
Output:
Hi Get Noob
It just stops when it finds any nil value and the table order is the same as in the table
The order isn't specified in in pairs
while the order is specified in in ipairs
, which means the i in ipairs stands for index.