Lua 初學 table常用的公共方法

2021-10-09 23:24:30 字數 1345 閱讀 7337

1.table.insert(table1_name,table2_name) 將table2_name的值插入到table1_name

t1=,}

t2=table.

insert

(t1,t2)

2.table.remove(table_name,[index]),如果引數列表中沒有index的時候,移除的是table中的最後乙個元素,有index的時候移除的是table[index]的元素

table.

remove

(t1)

table.

remove

(t1,

1)

3.table.sort(table_name,[sortfunction]),如果引數列表沒有sortfunction的時候,使用的是預設的sortfunction,公升序排序,sortfunction是自定義的排序方法

類似於icompare中的實現,這裡常用於數字排序,或者其他元素的話,要自定義好排序的方法才進行使用

t3=

table.

sort

( t3)

table.

sort

( t3, function ( a,b )

if a>b then

return

true

endend )

4.table.concat(table_name,";",[start_index],[end_index]),拼接一般用於number和string中,返回的是string

t4=

strdemo=table.

concat

( t4,

";")

print strdemo的話會得到asd;qwe;456

5.demo

t1=,}

t2=table.

insert

(t1,t2)

table.

remove

(t1)

table.

remove

(t1,1)

t3=table.

sort

( t3)

table.

sort

( t3, function ( a,b )

if a>b then

return

true

endend )

t4=strdemo=table.

concat

( t4,

";")

lua的table庫中常用的函式

lua提供了一些輔助函式來操作table。例如,從list中insert和remove元素,對array的元素進行sort,或者concatenate陣列中的所有strings。下面就詳細地講解這些方法。insert and remove table.insert將乙個元素插入到指定位置,例如 t ...

Lua中table裡內嵌table的例子

廢話www.cppcns.com不多少,看代程式設計客棧碼 複製 如下 local temp insert table data local temp insert table n程式設計客棧bsp temp insert table data temp insert table data,temp...

lua的table長度問題

看以下 local tbltest1 print table.getn tbltest1 這段 輸出的結果是3,這個大家都知道,是吧。不管最後那個3後面有沒有加逗號,結果都是3。再看下面的 local tbltest2 print table.getn tbltest2 這段 輸出的結果是多少?這裡...