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

Can someone explain "in pairs" loop for me and "in ipairs"?

Asked by
Zeuxulaz 148
4 years ago

in pairs in pairs in pairs in pairs

1
Answer you accepted is not really correct, ipairs can be used in any scripts. Not module scripts, and it isn't mostly used there either. You don't use ipairs to prevent errors in module scripts either. pairs can iterate through an entire table regardless of key, and ipairs only iterates through keys that are integers. Something else that Anas left out. EzraNehemiah_TF2 3552 — 4y
0
I actually said "mostly used" instead of "always used" it is personal preference AnasBahauddin1978 715 — 4y

3 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

The other answers are... almost right, but they're not quite there.


The Table

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!


pairs

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".

pairs will iterate through an entire table as long as the value is not equal to nil. It will return their key and value.


ipairs

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.

ipairs will iterate through an entire table and will ignore values whose keys are not integers. The loop will terminate once a value in the table is nil.



Other ways to iterate through a table

next

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.

next is a primitive function in lua. It will return nil when the "next" key in the table has a nil value.


The "Old Fashioned Way"

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.

This does the same exact thing as ipairs except that the loop does not stop if there is a nil value, assuming that there are other values following it.



I hope this helps! -LordDragonZord

0
This helped me! iivSnooxy 248 — 4y
Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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

Log in to vote
-1
Answered by 4 years ago

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.

Answer this question