chapter 20 The Table Library

来源:互联网 发布:数据冗余导致的问题 编辑:程序博客网 时间:2024/04/30 04:29

20.1 Insert and Remove

{10,20,30}  table.insert(t,1,15) t will be {15,10,20,30}.

we call insert without a position, it inserts the
element in the last position of the array (and, therefore, moves no elements).


t = {}
for line in io.lines() do
table.insert(t, line)
end
print(#t) --> (number of lines read)


In Lua 5.0, this idiom was common. In more recent versions, I prefer the idiom
t[#t+1]=line to append elements to a list.


 
The table.remove function removes (and returns) an element from a given
position in an array, moving down other elements to close space. When called
without a position, it removes the last element of the array.


With these two functions, it is straightforward to implement stacks, queues,
and double queues. We can initialize such structures as t={}. A push operation
is equivalent to table.insert(t,x); a pop operation is equivalent to
table.remove(t). The call table.insert(t,1,x) inserts at the other end of the
structure (its beginning, actually), and table.remove(t,1) removes from this
end.
The last two operations are not particularly efficient, as they must move
elements up and down. However, because the table library implements these
functions in C, these loops are not too expensive, so that this implementation is
good enough for small arrays (up to some hundred elements, say).


20.2 Sort

table.sort;

It takes   the array to be sorted plus an optional order function. This order function takes
two arguments and must return true when the first argument should come first
in the sorted array.
If this function is not provided, sort uses the default less than
operation (corresponding to the ‘<’ operator).


sorttest={1,3,4,5,2,8};
table.sort(sorttest,function(t1,t2)
  return t1<t2;
end);
for i=1,#sorttest do
  print(sorttest[i]);
end

也就是默认是按从小到大排列的,,,

A common confusion happens when programmers try to order the indices of
a table. In a table, the indices form a set, and have no order whatsoever  也就是index 是不排列的,也就是不会传入sort function

if you want to sort the indices,let's take an example:

print("============");
test2={
a=123,--又一次忘记table 以字母作key, 不需要"" ,if you need "" ,then you have to add["key"],,,要么没有 要有就全有
c=45,
b=56,
f=17,
d=18,
e=19
}

indicesArray={};
for k,v in pairs(test2)  do
indicesArray[#indicesArray+1]=k;  --首先把indices 抽出了放到table里面
end
table.sort(indicesArray);  ---sort it

for _,v in pairs(indicesArray) do
 print(v,test2[v]);
end


==========

As a more advanced solution, we can write an iterator that traverses a table
following the order of its keys.



function pairsByKeyOrder(t,f)  --由此可以体会到enclosure 的封装性,,,
  local indicesArray={}; 
  for k,v in pairs(t)  do
    indicesArray[#indicesArray+1]=k;
    end
  table.sort(indicesArray,f);
  local i=0;
  return function ()
     i=i+1;
   return indicesArray[i],t[indicesArray[i]];
  end
end
local function sortDesc(t1,t2)
  return t1>t2; --从大到小排列
end
for k,v in pairsByKeyOrder(test2,sortDesc) do
print(k,v);
end


20.3 Concatenation

We have already seen table.concat in Section 11.6. It takes a list of strings
and returns the result of concatenating all these strings. An optional second
argument specifies a string separator to be inserted between the strings of the
list. The function also accepts two other optional arguments that specify the
indices of the first and the last string to concatenate


local tt={"a","c","sdf","efig","gef","hikj"};
print(table.concat(tt));
print(table.concat(tt,"=RP="));
print(table.concat(tt,"=HR=",2,5));


acsdfefiggefhikj
a=RP=c=RP=sdf=RP=efig=RP=gef=RP=hikj
c=HR=sdf=HR=efig=HR=gef


The next function is an interesting generalization of table.concat. It accepts
nested lists of strings:
function rconcat (l)
  if type(l) ~= "table" then return l end
     local res = {}
     for i = 1, #l do
     res[i] = rconcat(l[i])
     end
     return table.concat(res)
  end

For each list element, rconcat calls itself recursively to concatenate a possible
nested list. Then it calls the original table.concat to concatenate all partial
results.
print(rconcat{{"a", {" nice"}}, " and", {{" long"}, {" list"}}})
--> a nice and long list



























0 0
原创粉丝点击