Originally Posted by Sideshow
something i still did not read anywere:
what's the difference between pairs and ipairs ?
|
Pairs will iterate over all indexes in a table. Ipairs can only iterate over a table using integers as indexes. See
http://www.lua.org/pil/7.3.html
what is this "_" thing ?
would be great to know
|
Say a function returns multiple variables, and you only want the second variable returned. A perfect example would be UnitClass, the first variable is the localized name of the class, the second is the english name in capitals.:
Code:
function UnitClass(unit)
return "Mage", "MAGE"
end
local localClass, englishClass = UnitClass('player')
Right, so we now have both versions of the class... But what if you only need the second variable? You could use
select, but that will iterate over the variables returned, which is not necesary. So we use the underscore character. It's supposed to be a variable that nobody will use in it's code (who will ever name his variable _?). So we use:
Code:
local _, class = UnitClass('player')
if class == 'MAGE' then
-- Do some stuff here for mages only.
end
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 ...
|
Advice about that depends on the table structure and what you want to do with the information inside of it. List your code if you are unsure if you are doing it the most efficient way