Basic Functions

The following are basic global functions that are available in Lua. For security reasons, certain functions in the standard Lua distribution are not available. For more information see Changes to Lua.

以下是基本的全球可用的函數在Lua出於安全原因,某些功能在標準 Lua的分佈情況不詳。欲了解更多信息,請參閱變更到Lua

assert (v [, message])
Issues an error when the value of its argument v is false (i.e., nil or false); otherwise, returns all its arguments. message is an error message; when absent, it defaults to "assertion failed!"
發出錯誤時,它的參數值V是假的(即零或假),否則,返回其所有參數。消息是一個錯誤信息;如果不存在,則默認為“斷言失敗!“
總而言之,如果v是false or nil,那程式就會停止下來。
ex:
a=1
b=a
print(a)
print(b)
assert (a==b,"fuck you error message")--不會停止,因為a==b是回傳true
assert (a~=b,"fuck you error message")--會停止,因為a~=b是回傳false



error (message [, level])
Terminates the last protected function called and returns message as the error message. Function error never returns.
最後終止保護的功能要求,並傳回消息,該錯誤信息。函數永遠不會返回錯誤。
Usually, error adds some information about the error position at the beginning of the message. The level argument specifies how to get the error position. With level 1 (the default), the error position is where the error function was called. Level 2 points the error to where the function that called error was called; and so on. Passing a level 0 avoids the addition of error position information to the message.
通常情況下,增加了一些錯誤信息,錯誤位置在開始的消息。參數指定的水平如何得到錯誤位置。隨著級別 1(默認),錯誤位置就是錯誤的函數被調用。等級2點錯誤的地方稱為錯誤的函數被調用,等等。傳遞一個0級避免了另外的錯誤位置信息的消息。
ex:
function f2(n1, n2)
 local r = n1 / n2
 print(n1.."/"..n2.."="..r)
 if n2 == 0 then
  error("00000!!")  --false  印出檔案位置 和  00000!!
 else
  return r
 end
end

print(pcall(f2, 5, 0)) --印出5/0=1.#INF                                        
print(pcall(f2, 4, 2)) --印出4/2=2   true   2



_G
A global variable (not a function) that holds the global environment (that is, _G._G = _G). Lua itself does not use this variable; changing its value does not affect any environment, nor vice-versa. (Use setfenv to change environments.)
全局變量(不是函數),擁有全球環境(即_G._G=_G)。Lua 本身並不使用這個變量;改變其值不會影響任何環境,也反之亦然。 (使用setfenv改變環境。)


getfenv ([f])
Returns the current environment in use by the function. f can be a Lua function or a number that specifies the function at that stack level: Level 1 is the function calling getfenv. If the given function is not a Lua function, or if f is 0, getfenv returns the global environment. The default for f is 1.
返回當前環境中使用該函數。 F可一個 Lua函數或一個數字,指定在該協議棧的功能級別:級別1是函數調用 getfenv。如果給定的函數不是一個 Lua函數,或者如果f為 0,getfenv返回全球環境。默認為 F為 1。


getmetatable (object)
If object does not have a metatable, returns nil. Otherwise, if the object's metatable has a "__metatable" field, returns the associated value. Otherwise, returns the metatable of the given object.
如果對象沒有一元表,返回零。否則,如果對象的元表有一個“__metatable“字段,返回相關值。否則,返回給定對象的元表。


ipairs (t)
Returns three values: an iterator function, the table t, and 0, so that the construction
 for i,v in ipairs(t) do body end
返回三個值:迭代器的功能,表T,0,使施工
will iterate over the pairs (1,t[1]), (2,t[2]), ..., up to the first integer key absent from the table.
會遍歷對(1,t[1]),(2,t[2]),...,到第一個整數的關鍵不在表。
ex:
tbl = {"alpha", "beta", "gamma"}
for indx, value in ipairs(tbl) do
    print(indx, value)
 end
印出
1  alpha
2  beta
3  gamma



next (table [, index])
Allows a program to traverse all fields of a table. Its first argument is a table and its second argument is an index in this table. next returns the next index of the table and its associated value. When called with nil as its second argument, next returns an initial index and its associated value. When called with the last index, or with nil in an empty table, next returns nil. If the second argument is absent, then it is interpreted as nil. In particular, you can use next(t) to check whether a table is empty.
The order in which the indices are enumerated is not specified, even for numeric indices. (To traverse a table in numeric order, use a numerical for or the ipairs function.)
The behavior of next is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may however modify existing fields. In particular, you may clear existing fields.

允許程序遍歷所有領域的一個表。其第一個參數是一個表,其第二個參數是一個指標在此表。下一個返回下一個索引表及其關聯的值。當調用無作為第二個參數,接下來返回初始指標及其關聯值。當調用的最後一個索引,或無空表中,下返回nil。如果第二個參數是不存在,那麼它被解釋為零。特別是,您可以使用 next(t) 來檢查一個表是否為空。
順序列舉的指標沒有指定,即使是數字索引。 (遍歷表中的數字順序,可以使用一個數值或ipairs功能。)
明年的行為是未定義如果在遍歷,你指定任意值不存在的字段在表中。但是你可以修改現有的領域。尤其是,您可以清除現有的領域。


pairs (t)
Returns three values: the next function, the table t, and nil, so that the construction
     for k,v in pairs(t) do body end
返回三個值:下一個函數,表T和零,使施工
will iterate over all key–value pairs of table t.
會遍歷所有的鍵值對表T
See function next for the caveats of modifying the table during its traversal.
見函數的警告下一個表的修改在其遍歷。
ex:
tbl = {"alpha", "beta", ["one"] = "uno", ["two"] = "dos"}
 for key, value in pairs(tbl) do
   print(key, value)
 end
印出:
1 alpha
2 beta
one uno
two dos

pcall (f, arg1, ...)
Calls function f with the given arguments in protected mode. This means that any error inside f is not propagated; instead, pcall catches the error and returns a status code. Its first result is the status code (a boolean), which is false if the call succeeds without errors. In such case, pcall also returns all results from the call, after this first result. In case of any error, pcall returns false plus the error message.
調用函數 f與給定的參數在保護模式。這意味著,任何錯誤裡面 f是不會傳播,而是pcall捕獲該錯誤,並返回一個狀態代碼。它的第一個結果是狀態代碼(布爾),這是假的,如果調用成功時不出現錯誤。在這種情況下,pcall也返回所有結果從調用,在此之後的第一個結果。在案件的任何錯誤,pcall返回false加上錯誤信息。
ex:
function f2(n1, n2)
 local r = n1 / n2
 print(n1.."/"..n2.."="..r)
 if n2 == 0 then
  error("00000!!")  --false  印出檔案位置 和  00000!!
 else
  return r
 end
end

print(pcall(f2, 5, 0)) --印出5/0=1.#INF                                        
print(pcall(f2, 4, 2)) --印出4/2=2   true   2




print (...)
Receives any number of arguments, and prints their values to stdout, using the tostring function to convert them to strings. print is not intended for formatted output, but only as a quick way to show a value, typically for debugging. For formatted output, use string.format.
接收任意數量的參數,並打印它們的值到標準輸出,使用ToString函數將它們轉換為字符串。打印不適用於格式化輸出,但只是作為一個快捷方式來表明一個值,通常用於調試。對於格式化輸出,使用string.format。
ex:
它也會印出位址
function aaa()
   local b=0
end
print(aaa)  --結果是印出位址

////////////////////////////////////////////////////////////////////////////////////////////
rawequal (v1, v2)
Checks whether v1 is equal to v2, without invoking any metamethod. Returns a boolean.
檢查是否V1是等於 v2的,沒有調用任何元方法。返回一個布爾值。

rawget (table, index)
Gets the real value of table[index], without invoking any metamethod. table must be a table; index may be any value.
獲取真正的價值表[索引],沒有調用任何元方法。表必須是表,索引可以是任何值。
ex:
t1={}
t1[0]=10
t1[1]=20
print("vvvvv",rawget (t1, 1))  --印出20



rawset (table, index, value)
Sets the real value of table[index] to value, without invoking any metamethod. table must be a table, index any value different from nil, and value any Lua value.
設置實際價值表[index]來的價值,沒有調用任何元方法。表必須是表,索引不同於無任何價值,價值的任何Lua值。This function returns table.這個函數返回的表。
ex: 
t2={}
rawset (t2, 0,1100)
print(t2[0])



select (index, ...)
If index is a number, returns all arguments after argument number index. Otherwise, index must be the string "#", and select returns the total number of extra arguments it received.
如果索引是一個數字,返回後,所有參數指標的參數數目。否則,索引必須是字符串“#”,然後選擇返回總數收到額外的參數。
ex:
print(select (2,"sundure","kke","vvk"))  --印出 kke vvk

print(select (2,"sundure",#"kke","vvk"))  --印出 3,表示 kke 的長度

qq=select (1,#"sundure")
print("quee", qq) --印出7,表示sundure的長度

print(select ("#","sundure","kke","vvk","tto"))  --印出4,表示有4個參數



setfenv (f, table)
Sets the environment to be used by the given function. f can be a Lua function or a number that specifies the function at that stack level: Level 1 is the function calling setfenv. setfenv returns the given function.
As a special case, when f is 0 setfenv changes the environment of the running thread. In this case, setfenv returns no values.
設置環境要使用給定的函數。 F可一個 Lua函數或一個數字,指定在該協議棧的功能級別:級別1是函數調用 setfenv。 setfenv返回給定功能。
作為一個特殊的情況下,當 f為0 setfenv變化環境中的運行的線程。在這種情況下,setfenv返回任何值。


setmetatable (table, metatable)
Sets the metatable for the given table. (You cannot change the metatable of other types from Lua, only from C.) If metatable is nil, removes the metatable of the given table. If the original metatable has a "__metatable" field, raises an error.
This function returns table.

設置元表給定表。 (您不能更改,其他類型的元表從 Lua中,只有從 C)如果是零元表,元表中刪除給定的表。如果原來的元表有一個“__metatable“領域,將引發錯誤。
這個函數返回的表。


tonumber (e [, base])
Tries to convert its argument to a number. If the argument is already a number or a string convertible to a number, then tonumber returns this number; otherwise, it returns nil.

An optional argument specifies the base to interpret the numeral. The base may be any integer between 2 and 36, inclusive. In bases above 10, the letter 'A' (in either upper or lower case) represents 10, 'B' represents 11, and so forth, with 'Z' representing 35. In base 10 (the default), the number can have a decimal part, as well as an optional exponent part (see §2.1). In other bases, only unsigned integers are accepted.

嘗試轉換它的參數為一個數字。如果參數已經是一個數字或一個字符串轉換成一個數字,這個數字則 tonumber返回,否則返回nil。

一個可選的參數指定了相應的解釋的數字。該基地可以是任何整數2和36之間,具有包容性。在上述10個基地,字母'A'(在大寫或小寫)代表10,'乙'代表11人,等等,以'Z'的佔35。在基10(默認),這個數字可以有一個小數部分,以及一個可選的指數的一部分(見§2.1)。在其他基地,唯一的無符號整數被接受。


tostring (e)
Receives an argument of any type and converts it to a string in a reasonable format. For complete control of how numbers are converted, use string.format.

If the metatable of e has a "__tostring" field, then tostring calls the corresponding value with e as argument, and uses the result of the call as its result.

接收任何類型的參數,並將其轉換為字符串在一個合理的格式。對於完全控制如何數字轉換,使用string.format。

如果元表電子商務具有“__tostring“字段,然後調用相應的值的toString與 E作為參數,並使用該調用的結果作為其結果。


type (v)
Returns the type of its only argument, coded as a string. The possible results of this function are "nil" (a string, not the value nil), "number", "string", "boolean", "table", "function", "thread", and "userdata".
返回的類型它唯一的參數,編碼為字符串。可能的結果,這個函數是“無“(一個字符串,而不是值為零),“數”,“弦“,”布爾“,“表“,”功能“,“線程“和”用戶數據“。
ex:
lua提供了八種數據類型: nil, boolean, string, number, userdata, function, thread, table. 如果想看某個表達式是何種類型的, 可以使用type()函數, 它的返回值是string類型:
 print(type(10.4*3))  --印出 number
 print(type(type(10.4*3))) ----印出 string


unpack (list [, i [, j]])
Returns the elements from the given table. This function is equivalent to
返回從給定的元素表。此功能相當於

1      return list[i], list[i+1], ..., list[j]
 
except that the above code can be written only for a fixed number of elements. By default, i is 1 and j is the length of the list, as defined by the length operator (see §2.5.5).

除了上面的代碼只能寫入一個固定數量的元素。默認情況下,i是1,J是清單的長度,所定義的長度運算符(見 §2.5.5)。








arrow
arrow
    全站熱搜
    創作者介紹
    創作者 createps 的頭像
    createps

    遊戲人生 人生遊戲

    createps 發表在 痞客邦 留言(0) 人氣()