Lua是否提供了一个函数来使单词中的第一个字符为大写(如php中的ucfirst),如果是,如何使用它?
我想keywords[1]成为第一个字母大写.我已经读过string.upper它,但它使整个单词成为大写.
可以创建一个通过require函数返回多个结果的Lua模块吗?我正在编写package.loaders的扩展,我想知道我是否需要支持这种行为.
例如,采用以下模块,名为mod.lua:
print("module loading")
return "string1", "string2"
Run Code Online (Sandbox Code Playgroud)
以下脚本需要:
print("running script")
s1, s2 = require("mod")
print("s1: " .. tostring(s1))
print("s2: " .. tostring(s2))
Run Code Online (Sandbox Code Playgroud)
结果如下:
running script
module loading
s1: string1
s2: nil
Run Code Online (Sandbox Code Playgroud)
当我希望返回第二个字符串时.我不打算使用这样的行为,我意识到你可以通过返回一个表并解压缩它来复制它,我只是想知道它是否有意义(因为它是有效的Lua语法)而且我找不到在任何地方都有明确的答案.
在Lua中对条件进行检查是否为空的正确方法是什么?if x == ""并且f x ~= ""似乎没有工作.
我想从Lua中的字符串中删除所有空格.这是我尝试过的:
string.gsub(str, "", "")
string.gsub(str, "% ", "")
string.gsub(str, "%s*", "")
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用.如何删除所有空格?
我刚刚开始使用Lua,我想知道(因为我在网站上找不到它)如果Lua有一个OR运算符,就像其他语言中的那样||:
if (condition == true || othercondition == false) {
somecode.somefunction();
}
Run Code Online (Sandbox Code Playgroud)
而在Lua,有
if condition then
x = 0
end
Run Code Online (Sandbox Code Playgroud)
我如何在Lua中编写IF块来使用OR?
我曾经遇到过几个 地方,人们称之为collectgarbage() 两次来完成所有未使用的对象.
这是为什么?为什么单个电话不够?为什么不打三个电话?
当我尝试下面的代码(在Lua 5.2上)时,__gc只需一次调用就可以最终确定对象(意思是:它被调用)collectgarbage:
do
local x = setmetatable({},{
__gc = function() print("works") end
})
end
collectgarbage()
os.exit()
Run Code Online (Sandbox Code Playgroud)
这是否意味着一个电话就足够了?
我在最近阅读的一些Lua源文件中看到了这种类型的语法,这是什么意思,特别是第二对括号一个例子,在https://github.com/karpathy/char-rnn/blob中的第8行 /master/model/LSTM.lua
local LSTM = {}
function LSTM.lstm(input_size, rnn_size, n, dropout)
dropout = dropout or 0
-- there will be 2*n+1 inputs
local inputs = {}
table.insert(inputs, nn.Identity()()) -- line 8
-- ...
Run Code Online (Sandbox Code Playgroud)
https://github.com/torch/nn/blob/master/Identity.lua的源代码nn.Identity
**********更新**************
()()模式在火炬库'nn'中使用了很多.第一对括号创建容器/节点的对象,第二对括号引用依赖节点.
例如,y = nn.Linear(2,4)(x)表示x连接到y,并且变换是从1*2到1*4的线性.我只是理解它的用法,它的连接方式似乎可以通过下面的答案之一来回答.
无论如何,接口的使用在下面有详细记录. https://github.com/torch/nngraph/blob/master/README.md
我想知道来自命令行的用户何时按下control-c,这样我就可以节省一些东西.
我该怎么做呢?我看了,但还没有看到任何东西.
注意:我对lua有点熟悉,但我不是专家.我主要使用lua来使用库Torch(http://torch.ch/)
在Java LuaJ库,我想知道如何要求或通过Java中的LUA关闭另一个名为LUA脚本导入功能的LUA脚本.例如,这不起作用:
public static LuaValue runInputStreamLua(InputStream inputStream) throws Exception {
Prototype luaScriptPrototype = LuaC.instance.compile(inputStream, "");
Globals luaScriptStandardGlobals = JsePlatform.standardGlobals();
luaScriptStandardGlobals.loadfile("mycoolmathfunctions.lua");
LuaClosure luaClosure = new LuaClosure(luaScriptPrototype, luaScriptStandardGlobals);
return luaClosure.call();
}
Run Code Online (Sandbox Code Playgroud)
这里的输入流是指另一个lua的内容:
import 'mycoolmathfunctions'
-- or maybe require mycoolmathfunctions ?
return sum({1, 2, 3})
-- or maybe mycoolmathfunctions.sum({1, 2, 3}) ?
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
lua ×10
torch ×2
conditional ×1
if-statement ×1
java ×1
logic ×1
luaj ×1
luajit ×1
replace ×1
string ×1