View Single Post
04-24-14, 09:06 PM   #36
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by cokedrivers View Post
... learn all these name, func, v, k, and other symbols/terms and what they actually mean.
None of those are Lua language keywords (eg. "local", "and", "function"), Lua functions (eg. "tonumber", "pairs"), or WoW API functions (eg. "UnitName", "GetGameTime"), so that means they don't "actually mean" anything -- they're just variable names. :P

Obviously, a variable name like "func" would probably be pointing to a function value, but "name" could be a lot of things (a player name, a frame name, an item name, a guild name, an NPC name, a spell name, etc.). "k" and "v" are normally used in pairs loops to represent the keys and values, respectively:

Code:
-- Given this table:
local t = {
     cat = "meow",
     dog = "woof",
     pig = "oink",
}
-- This:
for k, v in pairs(t) do
    print(format("The %s says %s.", k, v))
end
-- Is the same as this:
for animal, sound in pairs(t) do
    print(format("The %s says %s.", animal, sound))
end
-- Is the same as this:
for refdsaf, uiopioj in pairs(t) do
    print(format("The %s says %s.", refdsaf, uiopioj))
end
Like using an underscore to name a variable whose value you don't care about, using k and v to name the variables pointing to the keys and values in a loop over a table is just a convention. There's nothing magical about those variable names, just as there's nothing magical about using an underscore as a variable name; it just means that the purpose of those variables is immediately obvious, whereas in the example above, it's not immediately obvious what a variable like "uipioj" might be pointing at until you read more of the code to get context.

Similarly, "i" is commonly used in loops to represent the iteration count:
Code:
for i = 1, 10 do
     print("This is loop iteration #" .. i)
end
However, you could just as easily use it for something else:

Code:
local k, v, i, name, func = GetFriendInfo(1)
... though I wouldn't recommend it, since in that context those variable names aren't meaningful and don't help you keep track of the information they contain.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote