Ipairs
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Ipairs
Can someone explain ipairs for me? I know the following:
ipairs uses the length of the table selected with 1 as the first value and the # of things in the table and the second.
ipairs uses the number in the table in some form. So, instead of for i=1, #h do print(h) end you can do something else with ipairs to make it simpler.
Sorry, but thank you. I believe these are true about ipairs. If not, feel free to correct me .
ipairs uses the length of the table selected with 1 as the first value and the # of things in the table and the second.
ipairs uses the number in the table in some form. So, instead of for i=1, #h do print(h) end you can do something else with ipairs to make it simpler.
Sorry, but thank you. I believe these are true about ipairs. If not, feel free to correct me .
"your actions cause me to infer your ego is the size of three houses" -finley
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Ipairs
As far as I know
is basically the same as
or
The only difference is that ipairs is slower, and more convenient.
Code: Select all
for i, v in ipairs(t) do
-- code here
end
Code: Select all
local i = 1
while true do
local v = t[i]
if not v then break end
-- code here
i = i + 1
end
Code: Select all
for i = 1, #t do
local v = t[i]
if not v then break end
-- code here
end
Re: Ipairs
Well, let's look at the syntax.
i and v are special variables (they can be called anything, but i/v is standard) that ipairs provides you with which refer to an element of the table you're looking through (in this case, t). i is the index of the element, and v is the element itself. So something like
would output
v, then, is equivalent to t. It's just a convenient shorthand because generally when you're iterating through a table you want to do things with the table's contents. Thus, you could rewrite the code in your post as:
Note that ipairs will iterate over every consecutive numeric index in the table, from 1 to wherever. So if your table's like this:
then running the table printing code above would output
But if the table looked like
then the output would look like
because 4 does not come after 2. If you then added
4 would be part of the chain again, since 3 comes after 2 and 4 comes after 3. Thus, the output would be
Code: Select all
for i,v in ipairs(t) do
-- ...
end
Code: Select all
t = {"hi", "bye", 42}
for i,v in ipairs(t) do
print(i..": "..v)
end
Code: Select all
1: hi
2: bye
3: 42
Code: Select all
for i,v in ipairs(h) do
print(v)
end
Code: Select all
t = {}
t[1] = "foo"
t[2] = "bar"
t[3] = "baz"
Code: Select all
1: foo
2: bar
3: baz
Code: Select all
t = {}
t[1] = "foo"
t[2] = "bar"
t[4] = "honk"
Code: Select all
1: foo
2: bar
Code: Select all
t[3] = "baz"
Code: Select all
1: foo
2: bar
3: baz
4: honk
Re: Ipairs
I've never understood why you would want to use ipairs.
feels much cleaner in my opinion
Code: Select all
t={"a","b","c"}
for i=1,#t do
print(i..": "..t[i])
end
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Ipairs
For the convenience of not having to index the table every time you need the value. Usually table names aren't as short as t. You could of assign the value into a local variable, but I don't think you would call that cleaner.T-Bone wrote:I've never understood why you would want to use ipairs.
feels much cleaner in my opinionCode: Select all
t={"a","b","c"} for i=1,#t do print(i..": "..t[i]) end
Re: Ipairs
Are these two methods equally fast by the way?BlackBulletIV wrote:For the convenience of not having to index the table every time you need the value. Usually table names aren't as short as t. You could of assign the value into a local variable, but I don't think you would call that cleaner.T-Bone wrote:I've never understood why you would want to use ipairs.
feels much cleaner in my opinionCode: Select all
t={"a","b","c"} for i=1,#t do print(i..": "..t[i]) end
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
Re: Ipairs
I usually use the normal
Code: Select all
for i,v in pairs(t)
- slime
- Solid Snayke
- Posts: 3166
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Ipairs
pairs and ipairs have slightly different uses.Astusvis wrote:I usually use the normalCode: Select all
for i,v in pairs(t)
Re: Ipairs
Explain this please, I get ipairs, may as well get in pairs now that it's mentioned, but thanks for you're help!slime wrote: pairs and ipairs have slightly different uses.
"your actions cause me to infer your ego is the size of three houses" -finley
- slime
- Solid Snayke
- Posts: 3166
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Ipairs
ipairs will only loop through the array part of a table (t[1], t[2], ...., t[100], etc), but it does it in the correct order. pairs, on the other hand, loops through all key/value pairs of a table, but the order is not guaranteed.LuaWeaver wrote:Explain this please, I get ipairs, may as well get in pairs now that it's mentioned, but thanks for you're help!slime wrote: pairs and ipairs have slightly different uses.
For example, if you had this table:
Code: Select all
tbl = {
"this is an array value",
"this is another array value",
notarrayvalue = true,
thegame = "hi",
"third array value",
}
Code: Select all
for i, v in ipairs(tbl) do print(i, v) end
Code: Select all
1 this is an array value
2 this is another array value
3 third array value
Code: Select all
for k,v in pairs(tbl) do print(k,v) end
Code: Select all
1 this is an array value
2 this is another array value
3 third array value
notarrayvalue true
thegame hi
Who is online
Users browsing this forum: Ahrefs [Bot] and 5 guests