Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How do you quote a block comment?

edited December 2012 in Code Sharing Posts: 8

This


--[[ sleeping code --]] str = debug.getinfo(1).source str = string.sub(str,string.find(str,"--[["),string.find(str,"--]]"))

produces an error. More specifically its a malformed pattern error. How do you quote a block comment's beginning and end separately without producing an error?

Tagged:

Comments

  • Jmv38Jmv38 Mod
    Posts: 3,295

    I'am not sure i read you correctly. To comment some block just do:

    --[[
    
    ]]
    
  • Posts: 371

    .@Jmv38, he is trying to put those characters in a string... Try concatenating the strings together ('-'..'-'..']'..']'). Or use escape characters...

  • dave1707dave1707 Mod
    edited December 2012 Posts: 7,749

    ..@kyuoibi I think this is what you're after. The first set of code will print "sleeping code" and the starting and ending comment lines. The second set of code will print only the "sleeping code" line. In order to use what's referred to as magic characters, they have to be proceeded by the % character. The magic characters are ^ $ ( ) % . [ ] * + - ? so you needed the % in front of each of your find pattern characters.


    --[[ sleeping code --]] -- this code will print the "sleeping code" line and the starting and ending comment lines. str = debug.getinfo(1).source str=string.sub(str,string.find(str,"%-%-%[%["),(string.find(str,"%-%-%]%]")+4))  print(str) -- this code will print only the "sleeping code" line. str = debug.getinfo(1).source str=string.sub(str,(string.find(str,"%-%-%[%[")+4),(string.find(str,"%-%-%]%]")-1))  print(str)
  • edited December 2012 Posts: 8

    thanks @dave1707
    Ive found that this works


    str = string.sub(str,string.find(str,'\--%[%[+'),string.find(str,'\\--]]+')+2)

    what I'm really going after is to quote the entire block comment.
    While this works for the first

     --[[
    sleeping code
    --]]
    
    
    --[[
    what the
    --]]
    
    str = debug.getinfo(1).source
    str = string.sub(str,string.find(str,'\--%[%[+'),string.find(str,'\\--]]+')+2)
    

    if there were a second or so block comment it will ignore it.

  • dave1707dave1707 Mod
    Posts: 7,749

    .. @kyuoibi Try this.


     --[[ sleeping code --]] --[[ what the --]] str = debug.getinfo(1).source a=1 while a~=nil do     x=string.find(str,"%-%-%[%[",a)     if x==nil then         return     end     y=string.find(str,"%-%-%]%]",a+1)     if y==nil then         return     end     a=y     str1=string.sub(str,x,y+4)     print(str1) end --[[ end of code --]]
  • works great thanks dave

  • dave1707dave1707 Mod
    edited December 2012 Posts: 7,749

    .. @kyuoibi Actually I have an error in the above program. The line with y+4 should be y+3 . If you change the above program and put both comments back to back with no space between them (see below), you will show the first - of the second comment at the end of the first comment. By changing the 4 to a 3, it works the correct way.


     --[[ sleeping code --]]--[[ what the --]] str = debug.getinfo(1).source a=1 while a~=nil do     x=string.find(str,"%-%-%[%[",a)     if x==nil then         return     end     y=string.find(str,"%-%-%]%]",a+1)     if y==nil then         return     end     a=y     str1=string.sub(str,x,y+3)     print(str1) end --[[ end of code --]]
Sign In or Register to comment.