當我們想從一個表中返回多個值時,我們使用table.unpack()函數。它接受一個列表並返回多個值。
用法
table.unpack{x,y,z,....}
示例
這個table.unpack()函數為我們提供了作為參數傳遞給它的所有值,但我們也可以按照下麵顯示的示例指定我們想要的值 -
a, b = table.unpack{1,2,3}
print(a, b)
在上麵的例子中,即使table.unpack()函數包含不同的值,即 1、2 和 3,我們隻存儲前兩個值,即,a和b和價值3將被丟棄。
輸出
1 2
示例
需要注意的是,如果我們不傳遞任何值,那麽列表中存在的所有值都將從table.unpack()函數。考慮下麵顯示的例子 -
print(table.unpack{1,2,3})
輸出
1 2 3
示例
我們也可以忽略元素並從列表中選擇特定的索引或定位元素,考慮下麵顯示的示例 -
_, b = table.unpack{-1,-2}
print(b)
輸出
-2
相關用法
- Lua table.pack()用法及代碼示例
- Lua string.char()用法及代碼示例
- Lua string.gsub()用法及代碼示例
- Lua string.byte()用法及代碼示例
- Lua io.popen()用法及代碼示例
- Lua string.lower()用法及代碼示例
- Lua string.format()用法及代碼示例
- Lua string.upper()用法及代碼示例
- Lua math.modf()用法及代碼示例
- Lodash _.isValidDate()用法及代碼示例
- Lodash _.isInteger()用法及代碼示例
- Lodash _.sampleSize()用法及代碼示例
- Lodash _.fromQuery()用法及代碼示例
- Lodash _.noConflict()用法及代碼示例
- Lodash _.Intersection()用法及代碼示例
- Lodash _.values()用法及代碼示例
- Less isstring()用法及代碼示例
- Lodash _.isLength()用法及代碼示例
- Lodash _.third()用法及代碼示例
- Lodash _.negate()用法及代碼示例
注:本文由純淨天空篩選整理自Mukul Latiyan大神的英文原創作品 table.unpack() function in Lua programming。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。