Ipairs

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
LuaWeaver
Party member
Posts: 183
Joined: Wed Mar 02, 2011 11:15 pm
Location: Ohio, USA

Ipairs

Post 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.
"your actions cause me to infer your ego is the size of three houses" -finley
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Ipairs

Post 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.
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: Ipairs

Post 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
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Ipairs

Post 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:
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Ipairs

Post 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.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Ipairs

Post 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?
User avatar
Astusvis
Prole
Posts: 33
Joined: Fri Aug 05, 2011 5:22 pm

Re: Ipairs

Post by Astusvis »

I usually use the normal

Code: Select all

for i,v in pairs(t)
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Ipairs

Post 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.
LuaWeaver
Party member
Posts: 183
Joined: Wed Mar 02, 2011 11:15 pm
Location: Ohio, USA

Re: Ipairs

Post 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!
"your actions cause me to infer your ego is the size of three houses" -finley
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Ipairs

Post 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
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 5 guests