Forum

> > CS2D > Scripts > Pairs and ipairs differences?
Forums overviewCS2D overview Scripts overviewLog in to reply

English Pairs and ipairs differences?

3 replies
To the start Previous 1 Next To the start

old Pairs and ipairs differences?

KenVo
User Off Offline

Quote
I cannot understand the explanations in the Lua manual, so what is the differences of pairs and ipairs?
Also, someone said that pair execute faster, why?

old Re: Pairs and ipairs differences?

Flacko
User Off Offline

Quote
pairs() iterates through all the keys in the table. However, it may not be in the exact same order you wish. ipairs(), on the other hand, iterates through numeric keys only.

1
2
3
4
local t = {1,2}
t[3] = 3
t[7] = 7
t["test"] = "hello world"
1
2
3
for k,v in pairs(t) do
	print("t["..k.."] = "..v)
end
Possible output:
t[1] = 1
t[2] = 2
t[3] = 3
t[7] = 7
t[test] = hello world
1
2
3
for k,v in ipairs(t) do
	print("t["..k.."] = "..v)
end
Actual Output:
t[1] = 1
t[2] = 2
t[3] = 3

They are both quite fast, with pairs being slightly faster than ipairs (if the ammount of keys iterated are the same, of course)
Anyways, a for loop is always faster.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview