【问题标题】:Scite Lua: string comparison raises "attempt to call a string value"?Scite Lua:字符串比较引发“尝试调用字符串值”?
【发布时间】:2010-10-27 19:08:39
【问题描述】:

尝试为 Scite 编写 Lua 脚本(类似于 lua-users wiki: Scite Comment Box),当我编写以下代码时:

fchars = string.sub(line, 1, 3)

if fchars == "//" or fchars == "##" 
  print "got it"
end 

...编译失败并显示“attempt to call a string value”。

我尝试了不同的变体,例如:

assert(ktest = (("//" == fchars) or ("##" == fchars)))

...在我看来,当我尝试使用logical operatoror”创建“复合”布尔表达式时,编译失败了。

 

那么,我将如何在 Lua 中进行上述检查?也许根本不支持上面的类 C 语法 - 我应该改用 match 之类的东西?

 

提前感谢您的任何回答,
干杯!

【问题讨论】:

    标签: lua


    【解决方案1】:

    以下对我来说很好用:

    line = "//thisisatest"
    
    fchars = string.sub(line, 1, 2) -- I assume you meant 1,2 since // and ##
                                    -- are only 2 characters long
    
    if fchars == "//" or fchars == "##" then -- you're missing 'then'
       print("got it!") 
    end
    

    【讨论】:

    • 谢谢约翰 - 似乎我们在同时写作,所以我一开始错过了你的答案 :) 顺便说一句,回复:“1,2” - 我在阅读“substring of a string s ranging from position i to j (inclusive)”后犯了那个错误在Programming in Lua : 27.2 中,记住这里的字符串是从 1 开始的 :) 干杯!
    【解决方案2】:

    Pfffft.... 语法错误 - 最后忘记了then

    if fchars == "//" or fchars == "##" then
      print "got it"
    end 
    

    干杯!

    【讨论】:

    • 注意 - 即使您记得then,也可能会出现此错误,但是您错误地使用了<>!= 作为不等关系运算符,而不是正确的~=Lua: 5.0 reference manual)...如果我可以添加的话,这是一个相当混乱的错误消息:)
    猜你喜欢
    • 2020-10-19
    • 2021-08-15
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 2016-08-26
    相关资源
    最近更新 更多