12.07.2015 Views

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

92 Part I ■ Learning to ProgramIn this example, the function foo() takes two arguments (arg1 <strong>and</strong> arg2)<strong>and</strong> returns a single value someReturn. These signatures can also indicateoptional arguments, by enclosing them in square brackets:somereturn = foo(arg1 [, arg2])This notation indicates that the second argument to foo() is optional. Whenyou see this, you should consult the description <strong>of</strong> the function <strong>and</strong> argumentsto determine the behavior <strong>of</strong> the function because it varies.Table LibraryThe table library provides several functions that allow you to easily addelements, remove elements, <strong>and</strong> sort array tables. In addition, a utility functionis provided that works outside <strong>of</strong> the array part <strong>of</strong> the table, returning themaximum numeric index used in the table. The <strong>for</strong>mer functions all operateexclusively on the array part <strong>of</strong> the table, whereas the latter can be used onany type <strong>of</strong> table.str = table.concat (table [, sep [, i [, j]]])The table.concat() function concatenates all entries <strong>of</strong> the array part <strong>of</strong>a table, with an optional separator string sep. Given an array where allelements are strings or numbers, it returns table[i]..sep..table[i+1] ...sep..table[j]. The default value <strong>for</strong> sep is the empty string, the default <strong>for</strong>i is 1, <strong>and</strong> the default <strong>for</strong> j is the length <strong>of</strong> the table. If i is greater than j, itreturns the empty string.> tbl = {“alpha“, “beta“, “gamma“}> print(table.concat(tbl, “:“))alpha:beta:gamma> print(table.concat(tbl, nil, 1, 2))alphabeta> print(table.concat(tbl, “\n“, 2, 3))betagammaThis function is an easy way to print the elements <strong>of</strong> the array part <strong>of</strong> atable. As you can see, sep can be any string (including the newline character)because it’s just concatenated with the entries in the table.table.insert (table, [pos,] value)The table.insert() function inserts a new element into the array, optionallyat position pos, shifting other elements up to make space, if necessary. Thedefault value <strong>for</strong> pos is n+1, where n is the length <strong>of</strong> the table. There<strong>for</strong>e, a call<strong>of</strong> table.insert(t,x) inserts x at the end <strong>of</strong> table t.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!