簡體   English   中英

為什么在Lua中對表進行排序不起作用

[英]why does sorting a table in Lua doesn't work

我有一段生成錯誤的Lua代碼,我不明白如何解決它。

  .............................
 local last_num = 0 
 local  channelTable={} 
 for num in channels.each_number() do  --  channels.each_number() returns 1.number  in each call 
  channelTable[last_num] =num;
  last_num = last_num +1;
 end    
 table.sort(channelTable);

基於lua文檔,我可以使用函數sortchannelTable保存的數字進行排序。 我得到的錯誤是:

attempt to index global 'table'

知道如何解決這個問題,或者應該實現冒泡排序? 謝謝你的暗示!

要么您沒有加載表庫,要么意外地覆蓋了它。

錯誤消息似乎被截斷:它應該說為什么索引失敗。

您看到的錯誤表明表庫不可用。 這個核心庫不太可能不屬於您的Lua環境,因此您可能已經在代碼中的其他位置分配了一些內容。

我認為問題可能是你期望在循環的每次迭代中調用channels.each_number()。 如果我沒有弄錯的話,我認為它只會在程序第一次通過循環時被調用。 無論你在for..in循環中使用什么,都需要成為一張桌子,我相信。 所以我想問題是你的表沒有按照你想要的那樣生成。 試着這樣做:

print('number of items in channelTable = ' .. #channelTable)

如果它出現0,那么我說的可能是問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM