Quantcast WoWInterface - View Single Post - Removing a table key?
View Single Post
09-09-14, 11:14 AM   #9
Sharparam
A Flamescale Wyrmkin
 
Sharparam's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2011
Posts: 101
Originally Posted by Phanx View Post
Using ipairs is never necessary. It doesn't do anything the above method doesn't do, but it is much slower. I don't even know why ipairs exists.
It might be worth mentioning they are not exactly equivalent (but the cases when they aren't are very rare special cases). As mentioned in a comment to this StackOverflow answer.

Example when a table has "holes":

Code:
ILUA: Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
"quit" to end

> t = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
> #t
10
> for i, v in ipairs(t) do io.write(v .. " ") end print() -- traditional for loop would have the same result
1 2 3 4 5 6 7 8 9 10

> t = {[1] = 1, [2] = 2, [4] = 4, [5] = 5}
> #t
5
> for i, v in ipairs(t) do io.write(v .. " ") end print()
1 2
> for i = 1, #t do io.write(t[i] .. " ") end print()
1 2 [string "local"]:1: attempt to concatenate field '?' (a nil value)
>
  Reply With Quote