lua-users home
lua-l archive

Re: Error in PiL3 in Listing 12.1 (page 121)

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi Egor
A correct code may look like the following:
function quote (s)
 -- find maximum length of sequences of equal signs
 local n = -1
 for w in string.gmatch(s, "]([]=]*)]") do
 for e in string.gmatch(w, "=*") do
 n = math.max(n, #e)
 end
 end
 -- The rest of the code is exactly the same as in the book
I test your code but it seems it does not handle all cases at all:
Lua 5.2.3 Copyright (C) 1994-2013 Lua.org, PUC-Rio
function quote(s)
 local n = -1
 for w in string.gmatch(s, "]([]=]*)]") do
 for e in string.gmatch(w, "=*") do
 n = math.max(n, #e)
 end
 end
 local eq = string.rep("=", n+1)
 return string.format(" [%s[\n%s]%s] ", eq, s, eq)
end
print(quote"]=]==")
 [==[
]=]==]==]
Regards,
Tomás

AltStyle によって変換されたページ (->オリジナル) /