有没有办法获得表的每个索引值?
例:
local mytbl = {
["Hello"] = 123,
["world"] = 321
}
Run Code Online (Sandbox Code Playgroud)
我想得到这个:
{"Hello", "world"}
Run Code Online (Sandbox Code Playgroud) 好吧所以我确定错误是真的很蠢,但由于某种原因我无法得到它,所以我非常感谢一些帮助:)
local function Selector()
x = math.random(1,2)
if x == 1 then
Veshtica
end
end
Run Code Online (Sandbox Code Playgroud)
"Veshtica"是我想要打电话的功能.错误消息 - ""="预计接近结束"
我是Lua的新手,我多次出现这个错误"'结束'预期(关闭'而在第3行')附近''"我已经检查过在网上找到答案但没有运气所以我希望有人可以帮我解决这个问题,非常感谢
这是我的代码:
print ("Welcome to the maze")
while input ~= "leave" do
print ("What do you want to do first? Leave or inspect?")
input = io.read()
if input == "inspect" then
print (" You venture towards the maze.")
end
if input == "leave" then
print ("You turn around and run.")
end
Run Code Online (Sandbox Code Playgroud) 我想用分隔符分割一个字符串,只得到最后一部分.我不关心其余的事情.我知道我能做到:
local last
for p in string.gmatch('file_name_test', '_%w+$') do last = p end
Run Code Online (Sandbox Code Playgroud)
哪个有效,但是,恕我直言,丑陋.
有没有更优雅的方式来说:
local last = string.gmatch('file_name_test', '_%w+$')[1]
Run Code Online (Sandbox Code Playgroud)
哪个不起作用,因为gmatch返回迭代器(而不是表).
这是片段:
local t = {}
t.tt = {}
function t.xx()
for i=1,10 do
t.tt[i] = i
end
end
for i=1,10 do
print(t.tt[i])
end
Run Code Online (Sandbox Code Playgroud)
print功能的结果都是.nil为什么所有元素t.tt都是零?
在iTorch我运行一段代码时,require nn我在Jupyter中遇到以下错误:
[string "require 'nn'..."]:1: module 'nn' not found:
no field package.preload['nn']
no file '/usr/local/share/lua/5.2/nn.lua'
no file '/usr/local/share/lua/5.2/nn/init.lua'
no file '/usr/local/lib/lua/5.2/nn.lua'
no file '/usr/local/lib/lua/5.2/nn/init.lua'
no file './nn.lua'
no file '/usr/local/lib/lua/5.2/nn.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './nn.so'
stack traceback:
/usr/local/share/lua/5.2/itorch/main.lua:166: in function </usr/local/share/lua/5.2/itorch/main.lua:159>
[C]: in function 'require'
[string "require 'nn'..."]:1: in main chunk
[C]: in function 'xpcall'
/usr/local/share/lua/5.2/itorch/main.lua:209: in function </usr/local/share/lua/5.2/itorch/main.lua:173>
(...tail calls...)
/usr/local/share/lua/5.2/lzmq/poller.lua:75: in function 'poll'
/usr/local/share/lua/5.2/lzmq/impl/loop.lua:307: in function 'poll'
/usr/local/share/lua/5.2/lzmq/impl/loop.lua:325: in function 'sleep_ex'
/usr/local/share/lua/5.2/lzmq/impl/loop.lua:370: in …Run Code Online (Sandbox Code Playgroud) 在Lua中有一个很好的功能,可以快速检查语句的结果是否nil与使用短路版本或对情况作出反应; 像这样的东西:
text = GetTextFromUser() or "default text"
Run Code Online (Sandbox Code Playgroud)
意为指定的返回值GetTextFromUser()来text,如果GetTextFromUser()返回nil,然后分配"default text"给text
这基本上是使用or操作员短路评估进行分配的好方法.
我想知道c#||运营商是否也有这样的能力.如果不是,实现相同行为的最短途径是什么?三元运算符?如果声明?
A = {}
function A:text()
return 100
end
print(A["text"]()) -- prints "100"
----------------------------------
A = {}
function A:text(value)
return value
end
print(A["text"](100)) -- prints "nil"
Run Code Online (Sandbox Code Playgroud)
有没有办法可以将值作为参数传递并返回相同的值?我需要遍历5个函数...
我想在NodeMCU内存上保存一个Lua程序.当NodeMCU在复位后完成启动并准备接收命令时,该脚本应该自动开始执行而不将NodeMCU连接到任何外部计算机(通过ESPlorer等).我仍然可以通过ESPlorer终止执行.非常感谢一个工作的例子.