List of functions:
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 subsequent elements are aligned when inserted and removed.
So when inserting and removing in the for loop, pay attention to whether the items are missing when inserted and removed:
Local t = {1,2,3,3,5,3,6} for i,v in Ipairs (t) does if v = = 3 then Table.remove (t,i) End end--error, fourth 3 is not removed, Ipairs internally maintains a variable record 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 does if t[i] = = 3 then Table.remove (t,i) i = I-1 End End--error, i=i-1 This code is useless, the value of I is always from 1 to #t, the value of modify I in the For loop does not work Local t = {1,2,3,3,5,3,6} for i= #t, 1,-1 does if t[i] = = 3 then Table.remove (t,i End end--correct, traverse from backward to local t = {1,2,3,3,5,3,6} local i = 1 W Hile t[i] do if t[i] = = 3 then Table.remove (t,i) Else i = i+1 End end--correct, control whether the value of I is increased
2. Concat can stitch the array portion of a table into a string, separated by a seq.
in Lua, where strings are stored in a different way than C, each string in Lua is a separate copy, and stitching two strings creates a new copy, which can affect performance if the stitching is particularly numerous:
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 C7/>local beginTime = Os.clock () local t = {} for I=1, 30000 do t[i] = i end local str = Table.conc at (T, "") local endTime = Os.clock () print (endtime-begintime)
3. Sort can sort the elements of the table array section, need to provide comp function, comp (A, B) if a should be in front of B, Comp returns TRUE. Note that in case of a==b, be sure to return false:
Local function comp (b) return a <= B end Table.sort (t,comp)-error, exception may occur: Atte MPT to compare number with nil local function comp (b) if a = = Nil or b = Nil Then Return False End return a <= b end Table.sort (t,comp)-error, may appear Exception: Invalid order function for sorting--this exception may not be reported, but the result is incorrect; A==b return true causes these problems because Table.sort does not do it when implementing a quick sort Boundary detection: for (;;) {while (Lua_rawgeti (l, 1, ++i), Sort_comp (L,-1,-2)) {//does not detect bounds, 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)) {//undetected boundary, J will always reduce 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 above code, 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 beyond the bounds attempt to compare number with nil; B makes a non-null judgment, and also throws an exception because I exceeds the bounds invalid order function for sorting
LUA Table Library functions Insert, remove, concat, sort details