View Single Post
04-22-10, 04:31 PM   #18
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,327
Originally Posted by Sideshow View Post
something i still did not read anywere:

what's the difference between pairs and ipairs ?
what is this "_" thing ?

would be great to know

also, in my addon, i use a standard for k,v in pairs(mytable) do
but i don't even use the v in the script .... I reckon it takes a "long" time for the cpu to find the v though ...
1) pairs() iterates through all entries of a table, don't expect any sort of order in which entries are found first. ipairs() iterates only through entries with numeric indices starting at 1 and continuing until it runs into a nil entry.

2) "_" is a legal variable name in Lua, most commonly used just to dump any returned values into that aren't needed.

3) You may omit "v" and it should only remember the keys. The function will still return a second value for what's contained in the iterated entry, but Lua will then toss it away since there's no variable to assign it to. The function will always run the same no matter if you assign all return values to a variable or not.

A note on the performance of for k,v in pairs(t), all core Lua functions as well as the WoW API are written purely in C. This is extremely fast compared to writing the same function and having it run through the interpreter. As far as iterating through tables, it takes the same amount of time to find the keys as it is to find the associated values.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote