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.

Chapter 4 ■ Working with Tables 61that the elements <strong>of</strong> the array are always numbered properly so the array partfunctions all work properly.Just because a function has return values doesn’t mean you have to doanything with them. You could just as easily call table.remove(), ignoringthe return value entirely.Sorting the Elements <strong>of</strong> an ArrayWhen an array contains basic elements such as strings <strong>and</strong> numbers that canbe easily compared, there is a st<strong>and</strong>ard library function to sort the list. Thesyntax <strong>of</strong> the table.sort() function follows:table.sort(tbl [, comp])The second argument to table.sort() is covered in detail in Chapter 5, butthe first argument is the table that you would like to sort. You can call thisfunction <strong>and</strong> pass it a table to be sorted as the first argument:> print_array(tmp)1 alpha2 beta3 gamma4 delta> table.sort(tmp)> print_array(tmp)1 alpha2 beta3 delta4 gammaBecause the values in this table are strings, they are sorted alphabetically,in ascending order (this is the default). If the table contained numbers, theywouldbesortedinthesameway.A simple sort like this won’t be effective <strong>for</strong> more complex values (such astables), or when the values in an array are mixed (such as strings <strong>and</strong> numbers).Chapter 5 will show you how to use the second argument to table.sort()tocustom tailor the sort function <strong>for</strong> these situations.Using Tables as NamespacesYou’ve already been introduced to a few functions that are grouped together:table.insert()table.remove()table.sort()

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

Saved successfully!

Ooh no, something went wrong!