Search code examples
lua

[[ strings ]] with nested [[ strings ]] in lua?


The example in https://www.lua.org/pil/2.4.html shows:

page = [[
    <HTML>
    <HEAD>
    <TITLE>An HTML Page</TITLE>
    </HEAD>
    <BODY>
     <A HREF="http://www.lua.org">Lua</A>
     [[a text between double brackets]]
    </BODY>
    </HTML>
    ]]

however this gives me an error on the / in </BODY>.

lua54: html.lua:9: unexpected symbol near '/'

has something changed since the book was published?


Solution

  • You must use another level of long bracket:

    page = [=[
        <HTML>
        <HEAD>
        <TITLE>An HTML Page</TITLE>
        </HEAD>
        <BODY>
         <A HREF="http://www.lua.org">Lua</A>
         [[a text between double brackets]]
        </BODY>
        </HTML>
        ]=]
    

    The header says "This first edition was written for Lua 5.0", this was changed in Lua 5.1:

    The long string/long comment syntax ([[string]]) does not allow nesting. You can use the new syntax ([=[string]=]) in these cases. (See compile-time option LUA_COMPAT_LSTR in luaconf.h.)