I don't think that's exactly how its used, but I see "for blah blah in ipairs do" in a lot of code and I think it would be useful.
What does the for/do loop do?
What are ipairs? How do they work?
What does "for i, v in ipairs do" mean?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: What does "for i, v in ipairs do" mean?
Sometimes, for some reasons, you need to loop trhough a collection of elements.
For loop is meant for that purpose.
will output
They offers better support to loop trough a table.
will have the same output as before.
Pairs will also act the same way, and have the same output.
Sometimes, values are not indexed with numeric keys, in a table.
In this case, ipairs won't work, but pairs will.
will output
Lua_Tables_Tutorial
Pairs/ipairs
There's an online interpreter for Lua here, you can try some snippets there to understand.
For loop is meant for that purpose.
Code: Select all
t = { 'a','b','c','d'}
for i = 1, #t do
print(i,t[i])
end
pairs/ipairs are iterators.1, 'a'
2, 'b'
3, 'c'
4, 'd'
They offers better support to loop trough a table.
Code: Select all
t = { 'a','b','c','d'}
for key,value in ipairs(t) do
print(key,value)
end
Pairs will also act the same way, and have the same output.
Sometimes, values are not indexed with numeric keys, in a table.
In this case, ipairs won't work, but pairs will.
Code: Select all
t = {}
t.x = 1
t.y = 2
t.z = 3
for key,value in pairs(t) do
print(key,value)
end
That's a basic example. You will find more intel here :"x",1
"y",2
"z",3
Lua_Tables_Tutorial
Pairs/ipairs
There's an online interpreter for Lua here, you can try some snippets there to understand.
Last edited by Roland_Yonaba on Sun Aug 19, 2012 1:45 am, edited 2 times in total.
Re: What does "for i, v in ipairs do" mean?
Alright. Thanks!
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 5 guests