Page 1 of 2

Ipairs

Posted: Tue Aug 09, 2011 3:35 am
by LuaWeaver
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 :3.

Re: Ipairs

Posted: Tue Aug 09, 2011 4:29 am
by BlackBulletIV
As far as I know

Code: Select all

for i, v in ipairs(t) do
  -- code here
end
is basically the same as

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
or

Code: Select all

for i = 1, #t do
  local v = t[i]
  if not v then break end
  -- code here
end
The only difference is that ipairs is slower, and more convenient.

Re: Ipairs

Posted: Tue Aug 09, 2011 4:30 am
by bmelts
Well, let's look at the syntax.

Code: Select all

for i,v in ipairs(t) do
    -- ...
end
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

Code: Select all

t = {"hi", "bye", 42}
for i,v in ipairs(t) do
    print(i..": "..v)
end
would output

Code: Select all

1: hi
2: bye
3: 42
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:

Code: Select all

for i,v in ipairs(h) do
    print(v)
end
Note that ipairs will iterate over every consecutive numeric index in the table, from 1 to wherever. So if your table's like this:

Code: Select all

t = {}
t[1] = "foo"
t[2] = "bar"
t[3] = "baz"
then running the table printing code above would output

Code: Select all

1: foo
2: bar
3: baz
But if the table looked like

Code: Select all

t = {}
t[1] = "foo"
t[2] = "bar"
t[4] = "honk"
then the output would look like

Code: Select all

1: foo
2: bar
because 4 does not come after 2. If you then added

Code: Select all

t[3] = "baz"
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

1: foo
2: bar
3: baz
4: honk

Re: Ipairs

Posted: Tue Aug 09, 2011 6:59 am
by T-Bone
I've never understood why you would want to use ipairs.

Code: Select all

t={"a","b","c"}
for i=1,#t do
    print(i..": "..t[i])
end
feels much cleaner in my opinion :neko:

Re: Ipairs

Posted: Tue Aug 09, 2011 9:25 am
by BlackBulletIV
T-Bone wrote:I've never understood why you would want to use ipairs.

Code: Select all

t={"a","b","c"}
for i=1,#t do
    print(i..": "..t[i])
end
feels much cleaner in my opinion :neko:
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.

Re: Ipairs

Posted: Tue Aug 09, 2011 12:38 pm
by T-Bone
BlackBulletIV wrote:
T-Bone wrote:I've never understood why you would want to use ipairs.

Code: Select all

t={"a","b","c"}
for i=1,#t do
    print(i..": "..t[i])
end
feels much cleaner in my opinion :neko:
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.
Are these two methods equally fast by the way?

Re: Ipairs

Posted: Tue Aug 09, 2011 2:31 pm
by Astusvis
I usually use the normal

Code: Select all

for i,v in pairs(t)

Re: Ipairs

Posted: Tue Aug 09, 2011 2:35 pm
by slime
Astusvis wrote:I usually use the normal

Code: Select all

for i,v in pairs(t)
pairs and ipairs have slightly different uses.

Re: Ipairs

Posted: Tue Aug 09, 2011 3:48 pm
by LuaWeaver
slime wrote: pairs and ipairs have slightly different uses.
Explain this please, I get ipairs, may as well get in pairs now that it's mentioned, but thanks for you're help!

Re: Ipairs

Posted: Tue Aug 09, 2011 4:09 pm
by slime
LuaWeaver wrote:
slime wrote: pairs and ipairs have slightly different uses.
Explain this please, I get ipairs, may as well get in pairs now that it's mentioned, but thanks for you're help!
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.

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",
}
then doing this:

Code: Select all

for i, v in ipairs(tbl) do print(i, v) end
will output this:

Code: Select all

1	this is an array value
2	this is another array value
3	third array value
However when you use pairs to iterate over the table, you will get all the keys and values, but in no guaranteed order. This:

Code: Select all

for k,v in pairs(tbl) do print(k,v) end
will output something like this:

Code: Select all

1	this is an array value
2	this is another array value
3	third array value
notarrayvalue	true
thegame	hi