tags:

views:

38

answers:

2

Hi all,

Trying to write a Lua script for Scite (something like lua-users wiki: Scite Comment Box), and when I write the following piece of code:

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

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

... the compilation fails with "attempt to call a string value".

I have tried different variants, such as:

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

... and it seems to me that compilation fails when I try to make a 'compound' boolean expression using the logical operator "or".

 

So, how would I do the above check in Lua? Maybe the C-like syntax as above is not supported at all - and I should use something like match instead?

 

Thanks in advance for any answers,
Cheers!

+1  A: 

Pfffft.... syntax error - forgot then at end:

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

Cheers!

sdaau
+3  A: 

The following worked fine for me:

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
John Ledbetter
Thanks John - seems we were writing at the same time, so I missed your answer at first :) Btw, re: "1,2" - I made that mistake after reading "`substring of a string s ranging from position i to j (inclusive)`" in [Programming in Lua : 27.2](http://www.lua.org/pil/27.2.html), and remembering here the strings are 1-based :) Cheers!
sdaau