LUA table學習筆記

來源:互聯網
上載者:User

標籤:code   log   移動元素   ipairs   nbsp   第一個   ...   區間   blog   

 

function printT( ... )     for i,v in ipairs(...) do    print(i,v)endendt1={}t2={}t3={}table.insert(t1,"t1")table.insert(t1,"t2")table.insert(t1,1,"t3")print "1 table.insert-----------"--printT(t1)print "2 table.concat-----------"t4=table.concat( t1, ", ", 1, #t1)--print (type(t4).."    "..t4)print "3 table.move-----------"--[[   http://www.2cto.com/kf/201602/489932.htmltable.move (a1, f, e, t [,a2])將表"a1"中從整數索引"f"到整數索引"e"之間(源區間)的元素     複製到表"a2"中整數索引"t"及之後的位置(目標區間),     表"a2"預設為"a1",目標區間與源區間可以重疊。--]]table.move(t1,1,2,1,t2)--printT(t1)print "-----------"--printT(t2)print "4 table.pack-----------"--[[ table.pack(···)     建立一個"table",並將參數作為元素以整數索引(1, 2, ...)插入"table"中。     並將"n"這個域儲存參數的總個數,返回所建立的"table"。]]t4=table.pack(1,3,5)printT(t4)--[[ table.remove (list [, pos])移除 list 中 pos 位置上的元素,並返回這個被移除的值。 當 pos 是在 1 到 #list 之間的整數時, 它向前移動元素 list[pos+1], list[pos+2], ···, list[#list] 並刪除元素 list[#list]; 索引 pos 可以是 #list + 1 ,或在 #list 為 0 時可以是 0 ;  在這些情況下,函數刪除元素 list[pos]。pos 預設為 #list, 因此調用 table.remove(l) 將移除表 l 的最後一個元素。]]print "-----------"table.remove(t4,1)printT(t4)print "5 table.pack-----------"--[[table.sort (list [, comp])在表內從 list[1] 到 list[#list] 原地 對其間元素按指定次序排序。 如果提供了 comp , 它必須是一個可以接收兩個列表內元素為參數的函數。  當第一個元素需要排在第二個元素之前時,返回真 (因此 not comp(list[i+1],list[i]) 在排序結束後將為真)。  如果沒有提供 comp, 將使用標準 Lua 操作 < 作為替代品。排序演算法並不穩定; 即當兩個元素次序相等時,它們在排序後的相對位置可能會改變。使用函數"comp"排序表"list"中的元素。     "comp"接收兩個列表內元素作為參數,     當第一個元素需要排在第二個元素之前時,返回"true",否則返回"false"。     如果沒有提供"comp",預設使用"<"(小於 操作符)比對元素。]]table.sort(t4,function (x1,x2 )    return x1>x2end)printT(t4)

 

LUA table學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.