2017-06-19 98 views
1

我學習Lua和可能並沒有對如何在語言作品很大的把握,但我試圖創建的字符串庫的分裂功能:Lua中返回表是零

string.split = function(str, delimiter) 

    -- A function which splits a string by a delimiter 
    values = {} 
    currentValue = "" 
    for i = 1, string.len(str) do 

     local character = string.sub(str, i, i) 
     if(character == delimiter) then 

      table.insert(values,currentValue) 
      currentValue = "" 

     else 

      currentValue = currentValue..character 

     end 

    end 

    -- clean up last item 
    table.insert(values,currentValue) 

    return vaules 

end 

values如果我在退貨之前打印出來,不是零,但如果我打電話給t = string.split("hello world", " "),t將是零。我不太清楚爲什麼我的桌子正在消失

回答

1

您在返回聲明中有錯字。

vaules 

而不是values

vaules當然是零。

另一個建議:儘可能在本地設置變量。

+0

天啊,非常感謝你。 – AlgoRythm

+0

@AlgoRythm狗屎發生;) 使用複製和粘貼和自動完成來最大限度地減少這種錯誤。特別是如果您正在調試代碼和打印值 – Piglet