創建數組我一直在試圖讓我一個數組是這樣的:的Lua通過代碼
["Some String"] = true,
["Some other string"] = true
etc
使用代碼。我不知道如何創建數組。我已經試過:
local tempArray
tempArray = {}
tempArray["Some String"] = true
但是,這並不工作,它最高審計機關tempArray =零。
我在做什麼錯?
創建數組我一直在試圖讓我一個數組是這樣的:的Lua通過代碼
["Some String"] = true,
["Some other string"] = true
etc
使用代碼。我不知道如何創建數組。我已經試過:
local tempArray
tempArray = {}
tempArray["Some String"] = true
但是,這並不工作,它最高審計機關tempArray =零。
我在做什麼錯?
我總是創建的陣列中的Lua,如:
local myArray = {
["Hello"] = 'World',
["Testing"] = '123'
}
這應該工作,如果它不請確保您的Lua的安裝是最新且可正常工作。
您發佈的代碼當然沒有錯。但是,在交互式環境中,它可能會出現奇怪的情況,具體取決於塊是如何收集並交給解析器的。
正如您所寫的,您聲明本地tempArray
,然後顯然使用它。如果這些行被輸入到交互式Lua提示符中,該提示符將每行作爲一個單獨的塊,則在第一行中創建的本地將被創建並丟棄。第二行將創建一個具有相同名稱的全局變量,第三行將使用全局來設置一個字段。您可以證明這一點是這樣的:
C:\Users\Ross>lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> local tempArray
> tempArray = {}
> tempArray["Some String"] = true
>
> table.foreach(tempArray,print)
Some String true
>
> print(type(_G.tempArray))
table
>
在這裏,我已經證明了表tempArray
存在並具有恰與價值true
的一個關鍵。通過打印_G.tempArray
的類型,我已經證明創建了一個全局變量。
通過使用do
... end
對強制交互式提示將整個塊視爲單個塊,我們都創建並使用局部變量。不幸的是,在大塊的end
之後,本地現在已經超出了範圍,不能再被看到。
C:\Users\Ross>lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> do
>> local tempArray
>> tempArray = {}
>> tempArray["Some String"] = true
>> end
> print(type(_G.tempArray))
nil
> table.foreach(tempArray,print)
stdin:1: bad argument #1 to 'foreach' (table expected, got nil)
stack traceback:
[C]: in function 'foreach'
stdin:1: in main chunk
[C]: ?
>
我不知道有足夠的瞭解魔獸世界權威說話,但很可能在一個腳本中聲明當地人可能有知名度和值持續有趣的問題。如果他們使用大量的沙箱腳本,則腳本中的全局變量可能對其他腳本不可見。
您是否試圖以交互方式運行? 'local'會將該變量隔離爲輸入的該塊(行)。但在你的例子中,'tempArray'然後將在下一行被全局定義。 – BMitch