Function List:
Table.insert (table,[POS,] value)
Table.remove (table[, POS])
Table.concat (table[, sep[, i[, J]]]
Table.sort (table[, comp])
1. Insert and remove can only be used for insertion and removal of array elements, and the following elements are aligned when inserting and moving out.
So when inserting and removing in a for loop, notice that there are some items missing from the insert and remove:
Copy Code code as follows:
Local t = {1,2,3,3,5,3,6}
For i,v in Ipairs (t) do
If v = = 3 Then
Table.remove (T,i)
End
End
--Error, fourth 3 is not removed, Ipairs will maintain a variable record of the location of the traversal, remove the third number 3, Ipairs the next return value is 5 instead of 3
Local t = {1,2,3,3,5,3,6}
For I=1, #t do
If t[i] = = 3 Then
Table.remove (T,i)
i = I-1
End
End
--Error, i=i-1 This code is not used, the value of I is always from 1 to #t, the value of the modify I in the For loop does not work
Local t = {1,2,3,3,5,3,6}
For i= #t, 1,-1 do
If t[i] = = 3 Then
Table.remove (T,i)
End
End
--right, traversing from the back forward
Local t = {1,2,3,3,5,3,6}
Local i = 1
While T[i] do
If t[i] = = 3 Then
Table.remove (T,i)
Else
i = i+1
End
End
--correct, oneself control whether the value of I is increased
2. Concat can concatenate the array part of a table into a string, separated by a seq in the middle.
In LUA, strings are stored in a different way than C, where each string in Lua is a separate copy, and stitching two strings produces a new copy, which can affect performance if the stitching operation is particularly numerous:
Copy Code code as follows:
Local begintime = Os.clock ()
Local str = ""
For I=1, 30000 do
str = str.. I
End
Local endtime = Os.clock ()
Print (Endtime-begintime)
--consumes 0.613 seconds, produces 30,000 string copies, but only the last one is useful
Local begintime = Os.clock ()
Local T = {}
For I=1, 30000 do
T[i] = i
End
Local str = table.concat (t, "")
Local endtime = Os.clock ()
Print (Endtime-begintime)
--consumes 0.024 seconds, uses the concat, the one-time string concatenation comes out, only then produces one string copy
3. Sort can order the elements of the table array part, you need to provide the comp function, comp (A, B) if a should be in front of B, comp to return true.
Note that for a==b, be sure to return false:
Copy Code code as follows:
Local function Comp (a,b)
Return a <= b
End
Table.sort (T,comp)
--Error, possible exception: attempt to compare number with nil
Local function Comp (a,b)
If a = = Nil or b = Nil Then
return False
End
Return a <= b
End
Table.sort (T,comp)
--Error, possible exception: Invalid order function for sorting
may not report this exception, but the result is wrong;
The reason A==b returns true causes these problems because Table.sort does not do boundary detection when implementing a quick sort:
for (;;) {
while (Lua_rawgeti (l, 1, ++i), Sort_comp (L,-1,-2)) {//No bounds detected, I will always increase
if (i>=u) lual_error (L, "Invalid order function for sorting");
Lua_pop (L, 1);
}
while (Lua_rawgeti (l, 1,--j), Sort_comp (L,-3,-1)) {//No bounds detected, J will always decrease
if (j<=l) Lual_error (L, "Invalid order function for sorting");
Lua_pop (L, 1);
}
if (j<i) {
Lua_pop (L, 3);
Break
}
Set2 (L, I, J);
}
Looking at the code above, if A==b returns true and several values on the boundary are equal, Sort_comp cannot prevent I from continuing to grow until an exception is thrown out of bounds attempt to compare number with nil; even if we do a and b non Null judgment, it will also cause an exception invalid order function for sorting because I exceed the bounds
What is a quick sort, how Lua implements a quick sort, can refer to the LUA source description, which is not much described here;