简体   繁体   English

如何在Lua函数上使用参数

[英]How to use parameters on Lua Functions

I am a beginner in Lua programming, and I want to understand the following: 我是Lua编程的初学者,我想了解以下内容:

On the statement below, why it is used these brackets ( [ and ] )? 在下面的语句中,为什么要使用这些括号( [] )? I did not find anything explaining it. 我没有发现任何解释。

conn:execute([[ 'MySQLSTATEMENT' ]])

Also, what if the function had one more parameter? 另外,如果该函数还有一个参数怎么办? Would it be like this: 会这样吗:

conn:execute('another parameter',[[ 'MySQLSTATEMENT' ]]).

(I took these examples from the link: http://www.tutorialspoint.com/lua/lua_database_access.htm ) (我从以下链接获取了这些示例: http : //www.tutorialspoint.com/lua/lua_database_access.htm

Double square brackets are used to specify literal strings in Lua. 双方括号用于在Lua中指定文字字符串。 These strings can contain multiple lines and interpret escape sequences as plaintext. 这些字符串可以包含多行,并将转义序列解释为纯文本。 As for parameters, they are treated no differently than any other value. 至于参数,对待它们与其他任何值都没有区别。 Your example is syntactically correct for a function with two parameters. 您的示例在语法上对于具有两个参数的函数是正确的。

This style is desirable when your strings contain characters which might otherwise have to be escaped manually, such as \\ , ' , and " . For example, it's far easier to read and write [[here's a "quote"]] than it is to write "here's a \\"quote\\"" or 'here\\'sa "quote"' . 当您的字符串包含否则可能必须手动转义的字符(例如\\'"时,此样式是理想的。例如, [[here's a "quote"]]读写比它要容易得多。写"here's a \\"quote\\""'here\\'sa "quote"'

We can delimit literal strings also by matching double square brackets [[...]]. 我们也可以通过匹配双方括号[[...]]来分隔文字字符串。 Literals in this bracketed form may run for several lines, may nest, and do not interpret escape sequences. 以方括号括起来的形式的文字可能会连续运行多行,可能会嵌套并且不会解释转义序列。 Moreover, this form ignores the first character of the string when this character is a newline. 此外,当此字符是换行符时,此形式将忽略字符串的第一个字符。 This form is especially convenient for writing strings that contain program pieces; 这种形式对于编写包含程序段的字符串特别方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM