Forum

> > CS2D > Scripts > Pairs and ipairs differences?
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Pairs and ipairs differences?

3 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Pairs and ipairs differences?

KenVo
User Off Offline

Zitieren
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?

alt Re: Pairs and ipairs differences?

Flacko
User Off Offline

Zitieren
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.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht